package com.truyen.service.impl;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gdata.client.photos.*;
import com.google.gdata.data.*;
import com.google.gdata.data.media.*;
import com.google.gdata.data.photos.*;
import com.google.gdata.util.AuthenticationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.truyen.model.TapThoModel;
import com.truyen.persistence.dao.ChapterTmpTruyenMaDao;
import com.truyen.persistence.dao.ChapterTmpTruyenTranhDao;
import com.truyen.persistence.dao.ChapterTmpTruyenKiemHiepDao;
import com.truyen.persistence.dao.ChapterTmpTruyenTrinhThamDao;
import com.truyen.persistence.dao.ChapterTmpTruyenTinhCamDao;
import com.truyen.persistence.dao.ChapterTruyenMaDao;
import com.truyen.persistence.dao.ChapterTruyenTranhDao;
import com.truyen.persistence.dao.ChapterTruyenKiemHiepDao;
import com.truyen.persistence.dao.ChapterTruyenTrinhThamDao;
import com.truyen.persistence.dao.ChapterTruyenTinhCamDao;
import com.truyen.persistence.dao.TruyenMaDao;
import com.truyen.persistence.dao.TruyenTranhDao;
import com.truyen.persistence.dao.TruyenKiemHiepDao;
import com.truyen.persistence.dao.TruyenTrinhThamDao;
import com.truyen.persistence.dao.TruyenTinhCamDao;
import com.truyen.persistence.entity.Account;
import com.truyen.persistence.entity.ChapterTmpTruyenMa;
import com.truyen.persistence.entity.ChapterTruyenMa;
import com.truyen.persistence.entity.ContentAnhDep;
import com.truyen.persistence.entity.LoaiAnhDep;
import com.truyen.persistence.entity.LoaiTruyenChu;
import com.truyen.persistence.entity.LoaiTruyenTranh;
import com.truyen.persistence.entity.SearchLoaiTruyenKiemHiep;
import com.truyen.persistence.entity.SearchLoaiTruyenTinhCam;
import com.truyen.persistence.entity.SearchLoaiTruyenTranh;
import com.truyen.persistence.entity.SearchLoaiTruyenTrinhTham;
import com.truyen.persistence.entity.TacGia;
import com.truyen.persistence.entity.ChapterTam;
import com.truyen.persistence.entity.ChapterTruyenTranh;
import com.truyen.persistence.entity.ChapterTruyenKiemHiep;
import com.truyen.persistence.entity.ChapterTruyenTrinhTham;
import com.truyen.persistence.entity.ChapterTruyenTinhCam;
import com.truyen.persistence.entity.ChapterTmpTruyenTranh;
import com.truyen.persistence.entity.ChapterTmpTruyenKiemHiep;
import com.truyen.persistence.entity.ChapterTmpTruyenTrinhTham;
import com.truyen.persistence.entity.ChapterTmpTruyenTinhCam;
import com.truyen.persistence.entity.LoaiLog;
import com.truyen.persistence.entity.AnhDep;
import com.truyen.persistence.entity.TruyenMa;
import com.truyen.persistence.entity.TruyenTranh;
import com.truyen.persistence.entity.TruyenKiemHiep;
import com.truyen.persistence.entity.TruyenTrinhTham;
import com.truyen.persistence.entity.TruyenTinhCam;
import com.truyen.service.AnhDepService;
import com.truyen.service.ChapterTruyenMaService;
import com.truyen.service.ContentAnhDepService;
import com.truyen.service.LoaiAnhDepService;
import com.truyen.service.LoaiTruyenChuService;
import com.truyen.service.LoaiTruyenTranhService;
import com.truyen.service.SearchLoaiTruyenKiemHiepService;
import com.truyen.service.SearchLoaiTruyenTinhCamService;
import com.truyen.service.SearchLoaiTruyenTranhService;
import com.truyen.service.SearchLoaiTruyenTrinhThamService;
import com.truyen.service.TacGiaService;
import com.truyen.service.ChapterTamService;
import com.truyen.service.ChapterTmpTruyenTranhService;
import com.truyen.service.ChapterTmpTruyenKiemHiepService;
import com.truyen.service.ChapterTmpTruyenTrinhThamService;
import com.truyen.service.ChapterTmpTruyenTinhCamService;
import com.truyen.service.LoaiLogService;
import com.truyen.service.TruyenMaService;
import com.truyen.service.TruyenTranhService;
import com.truyen.service.TruyenKiemHiepService;
import com.truyen.service.TruyenTrinhThamService;
import com.truyen.service.TruyenTinhCamService;
import com.truyen.service.JsoupService;
import com.truyen.service.LogJsoupService;
import com.truyen.service.UserService;
import com.truyen.utils.PicasawebAlbumUtil;
import com.truyen.utils.Unsigned;


@Service
public class JsoupServiceImpl implements JsoupService {

	static Logger logger=Logger.getLogger(JsoupServiceImpl.class);
	
	@Autowired
	private LoaiAnhDepService loaiAnhDepService;
	
	@Autowired
	private LoaiTruyenChuService loaiTruyenChuService;
	
	@Autowired
	private SearchLoaiTruyenKiemHiepService searchLoaiTruyenKiemHiepService;
	
	@Autowired
	private SearchLoaiTruyenTinhCamService searchLoaiTruyenTinhCamService;
	
	@Autowired
	private SearchLoaiTruyenTrinhThamService searchLoaiTruyenTrinhThamService;
	
	@Autowired
	private AnhDepService anhDepService;
	
	@Autowired
	private ContentAnhDepService contentAnhDepService;
	
	@Autowired
	private TruyenTranhService truyenTranhService;
	
	@Autowired
	private TruyenMaService truyenMaService;
	
	@Autowired
	private TruyenKiemHiepService truyenKiemHiepService;
	
	@Autowired
	private TruyenTrinhThamService truyenTrinhThamService;
	
	@Autowired
	private TruyenTinhCamService truyenTinhCamService;
	
	@Autowired
	private TacGiaService tacGiaService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private LoaiLogService loaiLogService;
	
	@Autowired
	private LogJsoupService logJsoupService;
	
	@Autowired
	private LoaiTruyenTranhService loaiTruyenTranhService;
	
	@Autowired
	private SearchLoaiTruyenTranhService searchLoaiTruyenTranhService;
	
	@Autowired
	private ChapterTamService chapterTamService;
	
	@Autowired
	private ChapterTmpTruyenTranhService chapterTmpTruyenTranhService;
	
	@Autowired
	private ChapterTmpTruyenKiemHiepService chapterTmpTruyenKiemHiepService;
	
	@Autowired
	private ChapterTmpTruyenTrinhThamService chapterTmpTruyenTrinhThamService;
	
	@Autowired
	private ChapterTmpTruyenTinhCamService chapterTmpTruyenTinhCamService;

	@Autowired
	private TruyenTranhDao truyenTranhDao;
	
	@Autowired
	private TruyenKiemHiepDao truyenKiemHiepDao;
	
	@Autowired
	private TruyenMaDao truyenMaDao;
	
	@Autowired
	private TruyenTrinhThamDao truyenTrinhThamDao;
	
	@Autowired
	private TruyenTinhCamDao truyenTinhCamDao;
	
	@Autowired
	private ChapterTruyenTranhDao chapterTruyenTranhDao;
	
	@Autowired
	private ChapterTruyenKiemHiepDao chapterTruyenKiemHiepDao;
	
	@Autowired
	private ChapterTruyenMaDao chapterTruyenMaDao;
	
	@Autowired
	private ChapterTruyenTrinhThamDao chapterTruyenTrinhThamDao;
	
	@Autowired
	private ChapterTruyenTinhCamDao chapterTruyenTinhCamDao;
	
	@Autowired
	private ChapterTmpTruyenTranhDao chapterTmpTruyenTranhDao;
	
	@Autowired
	private ChapterTmpTruyenKiemHiepDao chapterTmpTruyenKiemHiepDao;
	
	@Autowired
	private ChapterTmpTruyenMaDao chapterTmpTruyenMaDao;
	
	@Autowired
	private ChapterTmpTruyenTrinhThamDao chapterTmpTruyenTrinhThamDao;
	
	@Autowired
	private ChapterTmpTruyenTinhCamDao chapterTmpTruyenTinhCamDao;
	
	@Autowired
	private ChapterTruyenMaService chapterTruyenMaService;
	
	private boolean flagStopGetContentAnhDep = true;
	
	/*************************************************************
	 * 
	 * blog truyen
	 * Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html
	 * 
	 *************************************************************/
	
	//Lay comic blogtruyen
	@Async
	@Override
	public void getLinkSourceAllTruyenFromBlogTruyen(String key) {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup blogtruyen");
		try {
			Document doc = Jsoup.connect("http://blogtruyen.com/danhsach/tatca")
					.timeout(1000000)
					.header("Accept", "*/*")
				    .header("Accept-Encoding", "gzip, deflate")
				    .header("Accept-Language", "en-us,en;q=0.5")
				    .header("Cache-Control", "no-cache")
				    .header("Connection", "keep-alive")
				    .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
				    .header("Cookie", "commentMode=FB; btpop1=1; btpop2=1; btpop3=1; __utma=239848528.715833447.1357391630.1357391630.1357396894.2; __utmc=239848528; __utmz=239848528.1357391630.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); layer=1; __utmb=239848528.1.10.1357396894")
				    .header("Host", "blogtruyen.com")
				    .header("Referer", "http://blogtruyen.com/")
				    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
				    .header("X-Requested-With", "XMLHttpRequest")
				    .get();
			Elements paging = doc.select(".paging");
			Elements cPage =  paging.select(".page");
			int lp = 0;
			if(cPage.size() < 1){
				lp = 2;
			}else{
				Element cPageLast = cPage.last();
				Elements a = cPageLast.select("a");
				String href = a.attr("href");
				int star = href.lastIndexOf("(")+1;
				int end = href.length()-1;
				String page = href.substring(star, end);
				lp = Integer.parseInt(page) + 1;
			}//ket thuc if(cPage.size() < 1){
			for(int i=1;i<lp;i++){
//					Thread.sleep(15000);
				Document doc1 = Jsoup.connect("http://blogtruyen.com/partialDanhSach/listtruyen/")
						.header("Accept", "*/*")
					    .header("Accept-Encoding", "gzip, deflate")
					    .header("Accept-Language", "en-us,en;q=0.5")
					    .header("Cache-Control", "no-cache")
					    .header("Connection", "keep-alive")
					    .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
					    .header("Cookie", "commentMode=FB; btpop1=1; btpop2=1; btpop3=1; __utma=239848528.715833447.1357391630.1357391630.1357396894.2; __utmc=239848528; __utmz=239848528.1357391630.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); layer=1; __utmb=239848528.1.10.1357396894")
					    .header("Host", "blogtruyen.com")
					    .header("Referer", "http://blogtruyen.com/DanhSach/TatCa")
					    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						.data("X-Requested-With", "XMLHttpRequest")
						.data("key", key)
						.data("listOrCate", "list")
						.data("orderBy", "title")
						.data("page", Integer.toString(i))
						.timeout(1000000)
						.post();
//					Document doc1 = res.parse();
				Elements div = doc1.select("div .row");
				int j = div.size();
				for(int ii=0;ii<j;ii++){
					Element divCuaTenTruyen = div.get(ii);
					Element aTruyen = divCuaTenTruyen.select("a").first();
					String nameTruyen = aTruyen.text().replace("\"", "");
					String alias = Unsigned.boDau(nameTruyen);
					Element divHide = divCuaTenTruyen.nextElementSibling();
					String info = divHide.select("div[style^=text-align: justify; font-size: 12px;]").text();
					if(info!=null && !info.equals("")){
						info = info.replace("\"", "'").replace("<", "").replace(">", "").replace("(", "").replace(")", "").replace("^", "").trim();
					}//ket thuc if(info!=null && !info.equals("")){
					String soChuongStr =  divCuaTenTruyen.select("div[style^=float: left; width: 85px; font-size: 13px;text-align: center]").text();
					int soChuong = Integer.parseInt(soChuongStr);
					String linkSource = "http://blogtruyen.com" + aTruyen.attr("href");
					linkSource = linkSource.replace(" ", "%20");
					TruyenTranh comic = truyenTranhService.findByProperty("alias",alias);
					if(comic==null){
						int soureBy = 1;
						Account account = userService.findById((long) 1);
						comic = new TruyenTranh(linkSource,nameTruyen,alias,soureBy,soChuong,info,account);
						truyenTranhService.save(comic);
					}else{
						comic.setTotalChapter(soChuong);
						truyenTranhDao.merge(comic);
						//xu ly gui email khi user dang ky theo doi truyen
					}//ket thuc if(comic==null){
				}//ket thuc for(int ii=0;ii<j;ii++){
			}//ket thuc for(int i=1;i<lp;i++){
			logJsoupService.writeLog("OK - BlogTruyen - Lấy truyện theo ký tự "+key,"Đã lấy xong ký tự "+key,loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - BlogTruyen - Lấy truyện theo ký tự "+key,"Có lỗi xảy ra",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
		getImagevLstChapterOfComicBlogTruyen();
	}//ket thuc ham
	
	//lay hinh anh va up len picasa, va vai thong tin khac thong tin cua comic
	@Async
	@Override
	public void getImagevLstChapterOfComicBlogTruyen(){
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup blogtruyen");
		PicasawebService myService = new PicasawebService("zaza");
		try {
			myService.setUserCredentials("truyenzcom1@gmail.com", "11062010");
		} catch (AuthenticationException e1) {
			logJsoupService.writeLog("ERROR - Kết nối với picasa","Kết nối với picasa bị lỗi",loaiLog,true);
			e1.printStackTrace();
		}
		List<TruyenTranh> lstComic = truyenTranhService.getLstTruyenBySourceBy(1);
//		List<TruyenTranh> lstComic = truyenTranhService.getLstComicLinkImageNullBlogTruyen(1);
		if(lstComic.size() > 0){
			int sizeLstComic = lstComic.size();
			for(int i = 0; i < sizeLstComic; i++){
				TruyenTranh comic = lstComic.get(i);
				try {
					Thread.sleep(1500);
					String linkSourceComic =  comic.getLinkSource();
					Document doc = Jsoup.connect(linkSourceComic)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.7,vi;q=0.3")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "commentMode=FB; btpop1=1; btpop2=1; btpop3=1; __utma=239848528.715833447.1357391630.1357391630.1357396894.2; __utmc=239848528; __utmz=239848528.1357391630.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); layer=1; __utmb=239848528.1.10.1357396894")
						    .header("Host", "blogtruyen.com")
						    .header("Referer", "http://blogtruyen.com/")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
							.get();
					Element cNdTruyen = doc.select("#content .nd_truyen").first();
					Elements divFirst = cNdTruyen.select("div[style^=margin:0 0 10px 0]");
					Elements div2 = cNdTruyen.select("div[style^=line-height: 15px;]");
					Elements conDiv2 = div2.select("div");
					int jj = conDiv2.size();
					boolean isFinished = comic.getIsFinished();
					if(!isFinished){
						String trangThai = "Đang cập nhật";
						trangThai = div2.select("div").eq(jj-1).select("span").text();
						if(trangThai.equals("Đã hoàn thành") || trangThai.equals("Hoàn thành")){
							isFinished = true;
						}
						comic.setIsFinished(isFinished);
					}//ket thuc if(!isFinished){
					TacGia tacGia = comic.getTacGia();
					if(tacGia == null){
						String nameAuthor = div2.select("div a[href^=javascript:linkTimNhanh(\"TenTacGia\"]").text();
						if(nameAuthor != null && !nameAuthor.equals("")){
							String authorAlias = Unsigned.boDau(nameAuthor);
							tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias, 1);
							if(tacGia == null){
								tacGia = new TacGia(nameAuthor,authorAlias,1);
								tacGiaService.save(tacGia);
							}
						}else{
							tacGia = tacGiaService.findById((long) 1);
						}
						comic.setTacGia(tacGia);
					}
					String categories = comic.getCategories();
					if(categories == null || categories.equals("")){
						categories = "";
						Elements theLoai = conDiv2.select("a[href^=/TheLoai/]");
						int jjj = theLoai.size();
						for(int iii=0;iii<jjj;iii++){
							if(iii == jjj-1){
								//xet de add the loai truyen tranh vao bang truyen tranh
								categories += theLoai.get(iii).text();
							}else{
								categories += theLoai.get(iii).text()+",";
							}//ket thuc if(iii == jjj-1){
							//kiem tra de them loai truyen tranh
							String category = theLoai.get(iii).text();
							String categoryAlias = Unsigned.boDau(category);
							LoaiTruyenTranh loaiTruyenTranh = loaiTruyenTranhService.findByProperty("alias", categoryAlias);
							if(loaiTruyenTranh == null){
								loaiTruyenTranh = new LoaiTruyenTranh(category,categoryAlias);
								loaiTruyenTranhService.save(loaiTruyenTranh);
							}//end if(loaiTruyenTranh == null){
							SearchLoaiTruyenTranh sltt = searchLoaiTruyenTranhService.findByLoaiTruyenTranhVTruyenTranh(loaiTruyenTranh,comic);
							if(sltt == null){
								sltt = new SearchLoaiTruyenTranh(loaiTruyenTranh,comic);
								searchLoaiTruyenTranhService.save(sltt);
							}
						}//ket thuc for(int iii=0;iii<jjj;iii++){
						comic.setCategories(categories);
					}//ket thuc if(categories == null || categories.equals("")){
					Elements form6 = doc.select("form#form5");
					String form6Action = form6.attr("action").replace("BinhChon", "sort_list_chap/1");
					if(form6Action!=null){
						Document docListChapter = Jsoup.connect("http://blogtruyen.com"+form6Action)
								.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
							    .header("Accept-Encoding", "gzip, deflate")
							    .header("Accept-Language", "en-us,en;q=0.7,vi;q=0.3")
							    .header("Cache-Control", "max-age=0")
							    .header("Connection", "keep-alive")
							    .header("Cookie", "commentMode=FB; btpop1=1; btpop2=1; btpop3=1; __utma=239848528.715833447.1357391630.1357391630.1357396894.2; __utmc=239848528; __utmz=239848528.1357391630.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); layer=1; __utmb=239848528.1.10.1357396894")
							    .header("Host", "blogtruyen.com")
							    .header("Referer", "http://blogtruyen.com/")
							    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
								.timeout(1000000)
							    .get();
						Elements lstChapter = docListChapter.select(".link_chapter");
						if(lstChapter.size() > 0){
							int sizeLstChapter = lstChapter.size();
							float chapterNumber = comic.getTotalChapter();
							for(int iiii=0;iiii < sizeLstChapter;iiii++){
								Elements aLink = lstChapter.get(iiii).select("a");
								String urlChap = "http://blogtruyen.com"+aLink.attr("href").replace(" ", "");
								String chapName = aLink.text();
								String chapNameUsigned = Unsigned.boDau(chapName);
//									chapterNumber = chapterNumber + 1;
								chapterNumber = iiii + 1;
								ChapterTmpTruyenTranh chapterTmpTruyenTranh = chapterTmpTruyenTranhService.findAliasvTruyen(comic, chapNameUsigned);
								ChapterTam chapter = chapterTamService.findAliasvTruyen(comic,chapNameUsigned);
								if(chapterTmpTruyenTranh == null && chapter==null){
									chapter = new ChapterTam(comic,chapName,chapNameUsigned,urlChap,chapterNumber,1);
									chapterTamService.save(chapter);
								}//ket thuc if(chapterTruyen == null && chapter==null){
							}//ket thuc for(int iiii=0;iiii<sizeLstChapter;iiii++){
						}//ket thuc if(lstChapter.size() > 0){
					}//ket thuc if(form6Action!=null && idTruyen!=null){ dung de lay list chapter
					String linkImage = comic.getLinkImage();
					if(linkImage == null || linkImage.equals("")){
						Element eimage = divFirst.select("div[style^=text-align:center;] a img").first();
						if(eimage!=null){
							String nameNewFileImage = "truyen-tranh-"+comic.getAlias().replace(" ", "-");
							String image = eimage.attr("src");
							image = image.replace(" ", "%20");
							URL fileURL = new URL(image);
							URLConnection urlConn = fileURL.openConnection();
						    urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)");
						    InputStream inputStream = null;
						    try {
						    	inputStream = urlConn.getInputStream();
						    	byte[] imageBytes = IOUtils.toByteArray(inputStream);
								//khai bao url album picasa dua theo user va albumid cua picasa
								//albumTruyenTranh
								URL albumPostUrlComic = new URL("https://picasaweb.google.com/data/feed/api/user/110519699166210558008/albumid/5803577621171257889");
								PhotoEntry myPhotoComic = new PhotoEntry();
								//dat ten cho tam anh khi up len picasa
								myPhotoComic.setTitle(new PlainTextConstruct(nameNewFileImage));
								//chuyen doi anh thanh .jpg
								MediaByteArraySource myMediaComic = new MediaByteArraySource(imageBytes, "image/jpeg");
								myPhotoComic.setMediaSource(myMediaComic);
								//tien hanh upload anh len picasa
								PhotoEntry returnedPhoto = myService.insert(albumPostUrlComic, myPhotoComic);
								String newSrc = returnedPhoto.getHtmlLink().getHref();
								if (returnedPhoto.getMediaContents().size() > 0) {
									newSrc = returnedPhoto.getMediaContents().get(0).getUrl();
									int endNewSrc = newSrc.lastIndexOf("/");
									String urlGooglePicasa = newSrc.substring(0, endNewSrc);
									urlGooglePicasa = urlGooglePicasa.replace("https://lh3.googleusercontent.com", "http://1.bp.blogspot.com")
									.replace("https://lh4.googleusercontent.com", "http://2.bp.blogspot.com")
									.replace("https://lh5.googleusercontent.com", "http://3.bp.blogspot.com")
									.replace("https://lh6.googleusercontent.com", "http://4.bp.blogspot.com")
									.replace("lh3.ggpht.com", "1.bp.blogspot.com")
									.replace("lh4.ggpht.com", "2.bp.blogspot.com")
									.replace("lh5.ggpht.com", "3.bp.blogspot.com")
									.replace("lh6.ggpht.com", "4.bp.blogspot.com");
									comic.setLinkImage(urlGooglePicasa);
									comic.setNameImage(nameNewFileImage);
								}//ket thuc if (returnedPhoto.getMediaContents().size() > 0) {
							} catch (Exception e) {
								comic.setLinkImage("http://1.bp.blogspot.com/-4oYsOZRo7Ro/UElc1Si1o1I/AAAAAAAAAO4/GkoWJeYohQo");
								comic.setNameImage(nameNewFileImage);
							}
						}//ket thuc if(eimage!=null){
					}//ket thuc if(linkImage == null || linkImage.equals("")){
					if(comic.getPostingTime() == null){
						comic.setPostingTime(new Date());
					}//ket thuc if(comic.getPostingTime() == null){
					truyenTranhDao.merge(comic);
//					logJsoupService.writeLog("OK - BlogTruyen - Cập nhật truyện "+comic.getName(),"Cập nhật truyện "+comic.getName()+" thành công",loaiLog,false);
				} catch (Exception e) {
					logJsoupService.writeLog("ERROR - BlogTruyen - Cập nhật truyện "+comic.getName(),"Có lỗi xảy ra khi lấy truyện "+comic.getName(),loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc for(int i = 0; i < sizeLstComic; i++){
			logJsoupService.writeLog("OK - BlogTruyen - Cập nhật truyện","Đã Cập nhật truyện",loaiLog,false);
		}//ket thuc if(lstComic.size() > 0){
		getContentChapterOfComicBlogTruyen();
	}//ket thuc ham
	

	//lay noi dung chapter
	@Async
	@Override
	public void getContentChapterOfComicBlogTruyen() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup blogtruyen");
//			PicasawebService myService = new PicasawebService("zaza");
//			try {
//				myService.setUserCredentials("truyenzcom1@gmail.com", "11062010");
//			} catch (AuthenticationException e1) {
//				logJsoupService.writeLog("ERROR - Kết nối với picasa","kết nối picasa thất bại",loaiLog,true);
//				e1.printStackTrace();
//			}
		int sourceBy = 1;
		List<ChapterTam> lstChapterTam = chapterTamService.getLstFromSourceByVTruyenNotNull(sourceBy,"truyenTranh");
		if(lstChapterTam.size() > 0){
			int sizeLstChapterTam = lstChapterTam.size();
			for(int i = 0; i < sizeLstChapterTam ; i++){
				ChapterTam chapterTam = lstChapterTam.get(i);
				String nameChapterTruyen = chapterTam.getName();
				TruyenTranh comic = chapterTam.getTruyenTranh();
				String nameChapterUnsigned = chapterTam.getAlias();
				String linkSource = chapterTam.getLinkSource();
				String content = "";
				try{
//						Thread.sleep(2000);
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "commentMode=FB; btpop1=1; btpop2=1; btpop3=1; __utma=239848528.715833447.1357391630.1357391630.1357396894.2; __utmc=239848528; __utmz=239848528.1357391630.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); layer=1; __utmb=239848528.1.10.1357396894")
						    .header("Host", "blogtruyen.com")
						    .header("Referer", chapterTam.getLinkSource())
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
							.get();
					Elements noiDungChuong = doc.select("#noidungchuong");
					Elements img = noiDungChuong.select("img");
					if(img.size() > 0){
					//tao album chua anh cua chapter
//								AlbumEntry myAlbum = new AlbumEntry();
//								URL postUrl = new URL("http://picasaweb.google.com/data/feed/api/user/110519699166210558008");
//								myAlbum.setTitle(new PlainTextConstruct(nameChapterUnsigned));
//								AlbumEntry insertedEntry = myService.insert(postUrl, myAlbum);
//								String hrefAlbum = insertedEntry.getFeedLink().getHref();
//								int lastHrefAlbum = hrefAlbum.lastIndexOf("?");
//								String hrefAlbumFeed = hrefAlbum.substring(0, lastHrefAlbum);
//								URL albumPostUrl = new URL(hrefAlbumFeed);
						int j = img.size();
						for(int ii = 0; ii < j; ii++){
							String src = img.get(ii).attr("src");
							if(!src.equals("http://3.bp.blogspot.com/-TBagud8Kn2o/TVo3b0jVChI/AAAAAAAAA7A/7QSW05H3KDY/s128/up.gif") || !src.equals("http://3.bp.blogspot.com/-hRNrAFmLRWE/TVo3bolAB5I/AAAAAAAAA7A/ccAsj8BU-Rk/s128/down.gif")){
								src = src.replace(" ", "%20");
//									URL fileURL = new URL(src);
//									URLConnection urlConn = fileURL.openConnection();
//								    urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)");
//									InputStream inputStream = urlConn.getInputStream();
//									String nameNewFileImage = "truyen-tranh-"+nameChapterUnsigned.replace(" ", "-")+"-trang-"+ii;
//									byte[] imageBytes = IOUtils.toByteArray(inputStream);
//									PhotoEntry myPhoto = new PhotoEntry();
//									myPhoto.setTitle(new PlainTextConstruct(nameNewFileImage));
//									MediaByteArraySource myMedia = new MediaByteArraySource(imageBytes, "image/jpeg");
//									myPhoto.setMediaSource(myMedia);
//									PhotoEntry returnedPhoto = myService.insert(albumPostUrl, myPhoto);
//									String newSrc = returnedPhoto.getHtmlLink().getHref();
//									if (returnedPhoto.getMediaContents().size() > 0) {
//										newSrc = returnedPhoto.getMediaContents().get(0).getUrl();
//										int endNewSrc = newSrc.lastIndexOf("/");
//										String urlGooglePicasa = newSrc.substring(0, endNewSrc);
//										urlGooglePicasa = urlGooglePicasa.replace("https://lh3.googleusercontent.com", "http://1.bp.blogspot.com")
//										.replace("https://lh4.googleusercontent.com", "http://2.bp.blogspot.com")
//										.replace("https://lh5.googleusercontent.com", "http://3.bp.blogspot.com")
//										.replace("https://lh6.googleusercontent.com", "http://4.bp.blogspot.com")
//										.replace("lh3.ggpht.com", "1.bp.blogspot.com")
//										.replace("lh4.ggpht.com", "2.bp.blogspot.com")
//										.replace("lh5.ggpht.com", "3.bp.blogspot.com")
//										.replace("lh6.ggpht.com", "4.bp.blogspot.com");
//										newSrc = urlGooglePicasa+"/s0/"+nameNewFileImage+".jpg";
//										String alt = "Truyện tranh "+nameChapterTruyen+" trang "+ii;
//										content += "<img src='"+newSrc+"' alt='"+alt+"' id='hinh"+ii+"' class='hinh' />" + "<br />";
//									}
								String alt = "Truyện tranh "+nameChapterTruyen+" trang "+ii;
								content += "<img src='"+src+"' alt='"+alt+"' id='hinh"+ii+"' class='hinh' />" + "<br />";
							}//ket thuc if(!src.equals("http://3.bp.blogspot.com/-TBagud8Kn2o/TVo3b0jVChI/AAAAAAAAA7A/7QSW05H3KDY/s128/up.gif") || !src.equals("http://3.bp.blogspot.com/-hRNrAFmLRWE/TVo3bolAB5I/AAAAAAAAA7A/ccAsj8BU-Rk/s128/down.gif")){
						}//ket thuc for(int ii = 0; ii < j; ii++){
					}else{
						Elements iFrame = noiDungChuong.select("iframe");
							String srcIframe = iFrame.attr("src");
						if(srcIframe !=null && !srcIframe.equals("")){
							content += "<iframe class='iFrameContent' width='853' height='480' src='"+srcIframe+"' frameborder='0' allowfullscreen></iframe>";
						}else{
							Elements embed = noiDungChuong.select("embed");
							String srcEmbed = embed.attr("src");
							if(srcEmbed !=null && !srcEmbed.equals("")){
								content += "<object class='objectContent' width='853' height='480'>";
								content += "<param name='movie' value='"+srcEmbed+"'></param>";
								content += "<param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param>";
								content += "<embed src='"+srcEmbed+"' type='application/x-shockwave-flash' width='853' height='480' allowscriptaccess='always' allowfullscreen='true'></embed>";
								content += "</object>";
							}//ket thuc if(srcEmbed !=null && !srcEmbed.equals("")){
						}//ket thuc if(srcIframe !=null && !srcIframe.equals("")){
					}//ket thuc if(img.size() > 0){
					if(!content.equals("")){
						float chapterNumber = chapterTam.getChapterNumber();
						Date postingTime = new Date();
						ChapterTruyenTranh chapterTruyen = new ChapterTruyenTranh(nameChapterTruyen,nameChapterUnsigned,chapterNumber,content,postingTime,comic);
						chapterTruyenTranhDao.save(chapterTruyen);
						ChapterTmpTruyenTranh chapterTmpTruyenTranh = new ChapterTmpTruyenTranh(chapterTruyen.getId(),nameChapterTruyen,nameChapterUnsigned,chapterNumber,comic);
						chapterTmpTruyenTranhDao.save(chapterTmpTruyenTranh);
						chapterTamService.delete(chapterTam.getId());
//						logJsoupService.writeLog("OK - Lấy nội dung "+chapterTruyen.getName(),"Đã lấy xong nội dung "+chapterTruyen.getName() +" (Chapter "+chapterTruyen.getChapterNumber()+")",loaiLog,false);
					}//ket thuc if(!content.equals("")){
				} catch (Exception e) {
					logJsoupService.writeLog("ERROR - Lỗi khi lấy nội dung chapter "+nameChapterTruyen,"LinkSource "+linkSource,loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc for(int i = 0; i < sizeLstChapterTam ; i++){
			logJsoupService.writeLog("OK - BlogTruyen - Cập nhật content truyện","Đã cập nhật xong content",loaiLog,false);
		}//ket thuc if(lstChapterTam.size() > 0){
	}//ket thuc ham

	/*************************************************************
	 * 
	 * lay truyen kiem hiep tu hixx.info
	 * 
	**************************************************************/
	
	//lay ten truyen kiem hiep,ten tac gia, link truyen theo the loai
	@Async
	@Override
	public void getLinkSourceTruyenKiemHiepFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			Document doc = Jsoup.connect("http://truyen.hixx.info/truyen/truyen-kiem-hiep/tat-ca.html")
					.timeout(1000000)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
				    .header("Accept-Encoding", "gzip, deflate")
				    .header("Accept-Language", "en-us,en;q=0.5")
				    .header("Cache-Control", "max-age=0")
				    .header("Connection", "keep-alive")
				    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmb=233845639.1.10.1345655004; __utmc=233845639; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345655010.1345655010.1; __utmb=202170061.6.10.1345655010; __utmc=202170061; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=3; cdt_max_clicks=4")
				    .header("Host", "mtruyen.hixx.info")
				    .header("Referer", "http://truyen.hixx.info/truyen/truyen-kiem-hiep/tat-ca.html")
				    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
				    .get();
			Elements li = doc.select("#content_left div.bg ul.top li");
			if(li.size() > 0){
				int sizeList = li.size();
				for(int i = 0; i < sizeList ; i++){
					Element item = li.get(i);
					Element aStory = item.select("a.story").first();
					String nameTruyen = aStory.text();
					String alias = Unsigned.boDau(nameTruyen);
					TruyenKiemHiep truyen = truyenKiemHiepService.findByProperty("alias", alias);
					Element spanChapterClear = item.select("span.chapter").first();
					String maxChapter = spanChapterClear.select("a.story").text().replace("-", "").replace(" ", "").trim();
					float totalChapter = Float.parseFloat(maxChapter);
					String finished = spanChapterClear.text();
					int beginIndex = finished.lastIndexOf("|")+1;
					finished = finished.substring(beginIndex, finished.length()).trim();
					boolean isFinished = false;
					if(finished.equals("Đủ bộ")){
						isFinished = true;
					}//ket thuc if(finished.equals("Đủ bộ")){
					if(truyen == null){
						String linkSource = aStory.attr("href");
						linkSource = linkSource.replace(" ", "%20");
						Element aLast = item.select("a").last();
						String nameAuthor = aLast.text();
						TacGia tacGia = tacGiaService.findByAliasLoaiTruyen("unknown", 2);
						if(nameAuthor != null && !nameAuthor.equals("")){
							String authorUnsigned = Unsigned.boDau(nameAuthor);
							tacGia = tacGiaService.findByAliasLoaiTruyen(authorUnsigned, 2);
							if(tacGia == null){
								tacGia = new TacGia(nameAuthor,authorUnsigned, 2);
								tacGiaService.save(tacGia);
							}
						}
						int soureBy = 2;
						Account account = userService.findById((long) 1);
						truyen = new TruyenKiemHiep(linkSource,nameTruyen,alias,soureBy,totalChapter,tacGia,isFinished,account);
						truyenKiemHiepService.save(truyen);
					}else{
						if(!truyen.getIsFinished()){
							truyen.setTotalChapter(totalChapter);
							truyen.setIsFinished(isFinished);
							truyenKiemHiepDao.merge(truyen);
						}
					}//ket thuc if(truyen == null){
//					logJsoupService.writeLog("OK - hixxinfo - Lấy truyện theo tất cả","Đã lấy xong truyện "+nameTruyen,loaiLog,false);
				}//ket thuc for(int i = 0; i < sizeList ; i++){
			}//ket thuc if(li.size() > 0){
			logJsoupService.writeLog("OK - hixxinfo - Lấy truyện","Đã lấy xong truyện kiếm hiệp",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - hixxinfo  - Lấy truyện","Có lỗi xảy ra khi lấy truyện kiếm hiệp",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
		getImageInfoChapterTruyenKiemHiepFromHixx();
	}//ket thuc ham

	//lay hinh anh va danh sach chuong
	@Async
	@Override
	public void getImageInfoChapterTruyenKiemHiepFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		PicasawebService myService = new PicasawebService("zaza");
		try {
			myService.setUserCredentials("truyenzcom1@gmail.com", "11062010");
		} catch (AuthenticationException e1) {
			logJsoupService.writeLog("ERROR - Kết nối với picasa","kết nối picasa thất bại",loaiLog,true);
			e1.printStackTrace();
		}//end try {
		List<TruyenKiemHiep> lstTruyen = truyenKiemHiepService.getLstTruyenBySourceBy(2);
//			List<Truyen> lstTruyen = truyenKiemHiepService.getLstComicLinkImageNullBlogTruyen("hixxinfo");
		if(lstTruyen.size() > 0){
			int sizeLstTruyen = lstTruyen.size();
			for(int i = 0; i < sizeLstTruyen; i++){
				TruyenKiemHiep truyen = lstTruyen.get(i);
				String linkSource = truyen.getLinkSource();
				try {
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345732336.1345745594.5; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=32; __utmc=202170061; cdt_max_clicks=3; __utmb=202170061.18.10.1345745594; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%2245f20e3a00a1c0e720ccc52c9684a3f3%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345745613%22%3B%7Dc252b482853d6d9a8cf810a96beae85b")
						    .header("Host", "mtruyen.hixx.info")
						    .header("Referer", "http://truyen.hixx.info/truyen/truyen-kiem-hiep/tat-ca.html")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element idTruyenTranhChiTiet = doc.select("#truyen_tranh_chi_tiet").first();
					String info = truyen.getInfo();
					if(info == null || info.equals("")){
						info = idTruyenTranhChiTiet.select(".desc").text().replace("<", "").replace(">", "").replace("\"", "'");
						truyen.setInfo(info.trim());
					}//ket thuc info == null || info.equals("")
					Date postingTime = truyen.getPostingTime();
					if(postingTime == null){
						postingTime = new Date();
						truyen.setPostingTime(postingTime);
					}//ket thuc postingTime == null
					Elements bt_pagination = idTruyenTranhChiTiet.select(".bt_pagination");
					//xu ly neu truyen co phan trang so chuong
					if(bt_pagination.size() > 0){
						String trang = bt_pagination.first().select("div:eq(0)").text().trim();
						int beginIndex = trang.lastIndexOf("/") + 1;
						String totalPage = trang.substring(beginIndex, trang.length()).trim();
						String hrefNext = bt_pagination.select(".next").first().select("a").first().attr("href");
						int endIndex = hrefNext.lastIndexOf("/");
						String hrefPage = hrefNext.substring(0, endIndex);
						int totalPag = Integer.parseInt(totalPage) + 1;
						for(int j = 1; j < totalPag ; j ++){
							Document docJ = Jsoup.connect(hrefPage+"/index"+j+".html")
									.timeout(1000000)
									.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
								    .header("Accept-Encoding", "gzip, deflate")
								    .header("Accept-Language", "en-us,en;q=0.5")
								    .header("Cache-Control", "max-age=0")
								    .header("Connection", "keep-alive")
								    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345732336.1345745594.5; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=47; __utmc=202170061; cdt_max_clicks=3; __utmb=202170061.48.10.1345745594; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22e74dd4fd8ff44e985fb7646aed01d956%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345750551%22%3B%7D54a39f03de3e8536923d8a1b79962d07")
								    .header("Host", "mtruyen.hixx.info")
								    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
								    .get();
							Element idTruyenTranhChiTietDocJ = docJ.select("#truyen_tranh_chi_tiet").first();
							Elements classDanhSach = idTruyenTranhChiTietDocJ.select("div.danh_sach");
							if(classDanhSach.size() > 0){
								int sizeDanhSach = classDanhSach.size();
								//bo 5 chuong dau nen dat iDanSach = 5
								for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
									Element aDanhSach = classDanhSach.get(iDanhSach).select("a").first();
									String linkSourceChapter = aDanhSach.attr("href");
									String textADanhSach = aDanhSach.text();
									int beginIndex1 = textADanhSach.indexOf("--", 2);
									String nameChapter = textADanhSach.substring(beginIndex1+2,textADanhSach.length()).trim();
									String nameChapterUnsigned = Unsigned.boDau(nameChapter);
									ChapterTmpTruyenKiemHiep chapterTmpTruyenKiemHiep = chapterTmpTruyenKiemHiepService.findAliasvTruyen(truyen, nameChapterUnsigned);
									ChapterTam chapterTam = chapterTamService.findAliasvTruyen(truyen,nameChapterUnsigned);
									if(chapterTmpTruyenKiemHiep == null && chapterTam==null){
										String chapterN = textADanhSach.substring(0,beginIndex1).replace("-", "").trim();
										int chapterNumber = Integer.parseInt(chapterN);
										chapterTam = new ChapterTam(truyen,nameChapter,nameChapterUnsigned,linkSourceChapter,chapterNumber,2);
										chapterTamService.save(chapterTam);
									}
								}//ket thuc vong lap for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
							}//ket thuc if classDanhSach.size() > 0
						}//ket thuc vong lap for(int j = 1; j < totalPag ; j ++){
					}else{
						Elements classDanhSach = idTruyenTranhChiTiet.select("div.danh_sach");
						if(classDanhSach.size() > 0){
							int sizeDanhSach = classDanhSach.size();
							for(int iDanhSach = 0; iDanhSach < sizeDanhSach; iDanhSach++){
								Element aDanhSach = classDanhSach.get(iDanhSach).select("a").first();
								String linkSourceChapter = aDanhSach.attr("href");
								String textADanhSach = aDanhSach.text();
								int beginIndex1 = textADanhSach.indexOf("--", 2);
								String nameChapter = textADanhSach.substring(beginIndex1+2,textADanhSach.length()).trim();
								String nameChapterUnsigned = Unsigned.boDau(nameChapter);
								ChapterTmpTruyenKiemHiep chapterTmpTruyenKiemHiep = chapterTmpTruyenKiemHiepService.findAliasvTruyen(truyen, nameChapterUnsigned);
								ChapterTam chapterTam = chapterTamService.findAliasvTruyen(truyen,nameChapterUnsigned);
								if(chapterTmpTruyenKiemHiep == null && chapterTam==null){
									String chapterN = textADanhSach.substring(0,beginIndex1).replace("-", "").trim();
									int chapterNumber = Integer.parseInt(chapterN);
									chapterTam = new ChapterTam(truyen,nameChapter,nameChapterUnsigned,linkSourceChapter,chapterNumber,2);
									chapterTamService.save(chapterTam);
								}
							}//ket thuc vong lap for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
						}//ket thuc if classDanhSach.size() > 0
					}//ket thuc xu ly neu truyen co phan trang so chuong if(bt_pagination.size() > 0){
					String linkImage = truyen.getLinkImage();
					if(linkImage == null){
						Element divClassImage = idTruyenTranhChiTiet.select(".image").first().select("img").first();
						if(divClassImage != null){
							String image = divClassImage.attr("src");
							if(image.equals("http://truyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg") || image.equals("http://mtruyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg")){
								truyen.setLinkImage("http://1.bp.blogspot.com/-4oYsOZRo7Ro/UElc1Si1o1I/AAAAAAAAAO4/GkoWJeYohQo");
								truyen.setNameImage(truyen.getAlias().replace(" ", "-")+".jpg");
							}else{
								image = image.replace(" ", "%20");
								URL fileURL = new URL(image);
								URLConnection urlConn = fileURL.openConnection();
							    urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)");
								InputStream inputStream = urlConn.getInputStream();
								String nameNewFileImage = "truyen-"+truyen.getAlias().replace(" ", "-");
								byte[] imageBytes = IOUtils.toByteArray(inputStream);
								//khai bao url album picasa dua theo user va albumId
								URL albumPostUrlComic = new URL("https://picasaweb.google.com/data/feed/api/user/110519699166210558008/albumid/5803577740902238801");
								PhotoEntry myPhotoComic = new PhotoEntry();
								//dat ten cho tam anh khi up len picasa
								myPhotoComic.setTitle(new PlainTextConstruct(nameNewFileImage));
								//chuyen doi anh thanh .jpg
								MediaByteArraySource myMediaComic = new MediaByteArraySource(imageBytes, "image/jpeg");
								myPhotoComic.setMediaSource(myMediaComic);
								//tien hanh upload anh len picasa
								PhotoEntry returnedPhoto = myService.insert(albumPostUrlComic, myPhotoComic);
								String newSrc = returnedPhoto.getHtmlLink().getHref();
								if (returnedPhoto.getMediaContents().size() > 0) {
									newSrc = returnedPhoto.getMediaContents().get(0).getUrl();
									int endNewSrc = newSrc.lastIndexOf("/");
									String urlGooglePicasa = newSrc.substring(0, endNewSrc);
									urlGooglePicasa = urlGooglePicasa.replace("https://lh3.googleusercontent.com", "http://1.bp.blogspot.com")
									.replace("https://lh4.googleusercontent.com", "http://2.bp.blogspot.com")
									.replace("https://lh5.googleusercontent.com", "http://3.bp.blogspot.com")
									.replace("https://lh6.googleusercontent.com", "http://4.bp.blogspot.com")
									.replace("lh3.ggpht.com", "1.bp.blogspot.com")
									.replace("lh4.ggpht.com", "2.bp.blogspot.com")
									.replace("lh5.ggpht.com", "3.bp.blogspot.com")
									.replace("lh6.ggpht.com", "4.bp.blogspot.com");
									truyen.setLinkImage(urlGooglePicasa);
									truyen.setNameImage(nameNewFileImage);
								}//ket thuc if (returnedPhoto.getMediaContents().size() > 0) {
							}//ket thuc if(image.equals("http://truyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg")){
						}//ket thuc divClassImage != null
					}//ket thu linkImage == null
					truyenKiemHiepDao.merge(truyen);
//					logJsoupService.writeLog("OK - Hixx.info - Cập nhật truyện "+truyen.getName(),"Cập nhật truyện "+truyen.getName()+" thành công",loaiLog,false);
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - Hixx.info - Lỗi cập nhật truyện kiếm hiệp"+truyen.getName(),"LinkSource "+linkSource,loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc vong lap for(int i = 0; i < sizeLstTruyen; i++){
			logJsoupService.writeLog("OK - Hixx.info - Cập nhật truyện","Đã cập nhật xong truyện kiếm hiệp",loaiLog,false);
		}//end if(lstTruyen.size() > 0){
		getContentChapterTruyenKiemHiepFromHixx();
	}//ket thuc ham

	//lay noi dung cua chuong
	@Async
	@Override
	public void getContentChapterTruyenKiemHiepFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		int sourceBy = 2;
		List<ChapterTam> lstChapterTam = chapterTamService.getLstFromSourceByVTruyenNotNull(sourceBy,"kiemHiep");
		if(lstChapterTam.size() > 0){
			int sizeLstChapterTam = lstChapterTam.size();
			for(int i = 0; i < sizeLstChapterTam; i++){
				ChapterTam chapterTam = lstChapterTam.get(i);
				String nameChapterTruyen = chapterTam.getName();
				String nameChapterUnsigned = chapterTam.getAlias();
				TruyenKiemHiep truyen = chapterTam.getTruyenKiemHiep();
				String linkSource = chapterTam.getLinkSource();
				String content = "";
				try {
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345832917.1345860864.9; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=10; cdt_max_clicks=4; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22bec81191a5c86ae3766cfd8451a9e905%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345860865%22%3B%7D93bee1ea31195046620d2e6ec395b87b; __utmb=202170061.6.10.1345860864; __utmc=202170061")
						    .header("Host", "mtruyen.hixx.info")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element idNoiDungChiTiet = doc.select("#ar-content-html").first();
					idNoiDungChiTiet.select("div[style^=float:right; padding: 10px]").remove();
					idNoiDungChiTiet.select("script").remove();
					idNoiDungChiTiet.select("noscript").remove();
					content = idNoiDungChiTiet.html();
					if(!content.equals("")){
						float chapterNumber = chapterTam.getChapterNumber();
						Date postingTime = new Date();
						ChapterTruyenKiemHiep chapterTruyen = new ChapterTruyenKiemHiep(nameChapterTruyen,nameChapterUnsigned,chapterNumber,content,postingTime,truyen);
						chapterTruyenKiemHiepDao.save(chapterTruyen);
						ChapterTmpTruyenKiemHiep chapterTmpTruyenKiemHiep = new ChapterTmpTruyenKiemHiep(chapterTruyen.getId(),nameChapterTruyen,nameChapterUnsigned,chapterNumber,truyen);
						chapterTmpTruyenKiemHiepDao.save(chapterTmpTruyenKiemHiep);
						chapterTamService.delete(chapterTam.getId());
//						logJsoupService.writeLog("OK - Hixx.info - Cập nhật xong chương  "+nameChapterTruyen,"Đã lấy xong nội dung "+nameChapterTruyen +" của truyện "+truyen.getName(),loaiLog,false);
					}//ket thuc if(!content.equals("")){
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - Hixx.info - Lỗi cập nhật nội dung truyện "+nameChapterTruyen,"LinkSource "+linkSource,loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc if(lstChapterTam.size() > 0){
		}//ket thuc vong lap for(int i = 0; i < sizeLstChapterTam; i++){
		logJsoupService.writeLog("OK - Hixx.info","Đã lấy xong nội dung của các chương truyện kiếm hiệp",loaiLog,false);
	}//ket thuc ham
	
	/*************************************************************
	 * 
	 * lay truyen tinh cam tu hixx.info
	 * 
	**************************************************************/
	
	//lay ten truyen kiem hiep,ten tac gia, link truyen theo the loai
	@Async
	@Override
	public void getLinkSourceTruyenTinhCamFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			Document doc = Jsoup.connect("http://truyen.hixx.info/truyen/truyen-tinh-cam/tat-ca.html")
					.timeout(1000000)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
				    .header("Accept-Encoding", "gzip, deflate")
				    .header("Accept-Language", "en-us,en;q=0.5")
				    .header("Cache-Control", "max-age=0")
				    .header("Connection", "keep-alive")
				    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmb=233845639.1.10.1345655004; __utmc=233845639; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345655010.1345655010.1; __utmb=202170061.6.10.1345655010; __utmc=202170061; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=3; cdt_max_clicks=4")
				    .header("Host", "mtruyen.hixx.info")
				    .header("Referer", "http://truyen.hixx.info/truyen/truyen-tinh-cam/tat-ca.html")
				    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
				    .get();
			Elements li = doc.select("#content_left div.bg ul.top li");
			if(li.size() > 0){
				int sizeList = li.size();
				for(int i = 0; i < sizeList ; i++){
					Element item = li.get(i);
					Element aStory = item.select("a.story").first();
					String nameTruyen = aStory.text();
					String alias = Unsigned.boDau(nameTruyen);
					TruyenTinhCam truyen = truyenTinhCamService.findByProperty("alias", alias);
					Element spanChapterClear = item.select("span.chapter").first();
					String maxChapter = spanChapterClear.select("a.story").text().replace("-", "").replace(" ", "").trim();
					float totalChapter = Float.parseFloat(maxChapter);
					String finished = spanChapterClear.text();
					int beginIndex = finished.lastIndexOf("|")+1;
					finished = finished.substring(beginIndex, finished.length()).trim();
					boolean isFinished = false;
					if(finished.equals("Đủ bộ")){
						isFinished = true;
					}//ket thuc if(finished.equals("Đủ bộ")){
					if(truyen == null){
						String linkSource = aStory.attr("href");
						linkSource = linkSource.replace(" ", "%20");
						Element aLast = item.select("a").last();
						String nameAuthor = aLast.text();
						TacGia tacGia = tacGiaService.findByAliasLoaiTruyen("unknown", 3);
						if(nameAuthor != null && !nameAuthor.equals("")){
							String authorUnsigned = Unsigned.boDau(nameAuthor);
							tacGia = tacGiaService.findByAliasLoaiTruyen(authorUnsigned, 3);
							if(tacGia == null){
								tacGia = new TacGia(nameAuthor,authorUnsigned, 3);
								tacGiaService.save(tacGia);
							}
						}
						int soureBy = 2;
						Account account = userService.findById((long) 1);
						truyen = new TruyenTinhCam(linkSource,nameTruyen,alias,soureBy,totalChapter,tacGia,isFinished,account);
						truyenTinhCamService.save(truyen);
					}else{
						if(!truyen.getIsFinished()){
							truyen.setTotalChapter(totalChapter);
							truyen.setIsFinished(isFinished);
							truyenTinhCamDao.merge(truyen);
						}
					}//ket thuc if(truyen == null){
//					logJsoupService.writeLog("OK - hixxinfo - Lấy truyện theo tất cả","Đã lấy xong truyện "+nameTruyen,loaiLog,false);
				}//ket thuc for(int i = 0; i < sizeList ; i++){
			}//ket thuc if(li.size() > 0){
			logJsoupService.writeLog("OK - hixxinfo - Lấy truyện","Đã lấy xong truyện tình cảm",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - hixxinfo  - Lấy truyện","Có lỗi xảy ra khi lấy truyện tình cảm",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
		getImageInfoChapterTruyenTinhCamFromHixx();
	}

	//lay hinh anh va danh sach chuong
	@Async
	@Override
	public void getImageInfoChapterTruyenTinhCamFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		PicasawebService myService = new PicasawebService("zaza");
		try {
			myService.setUserCredentials("truyenzcom1@gmail.com", "11062010");
		} catch (AuthenticationException e1) {
			logJsoupService.writeLog("ERROR - Kết nối với picasa","kết nối picasa thất bại",loaiLog,true);
			e1.printStackTrace();
		}//end try {
		List<TruyenTinhCam> lstTruyen = truyenTinhCamService.getLstTruyenBySourceBy(2);
//			List<Truyen> lstTruyen = truyenTinhCamService.getLstComicLinkImageNullBlogTruyen("hixxinfo");
		if(lstTruyen.size() > 0){
			int sizeLstTruyen = lstTruyen.size();
			for(int i = 0; i < sizeLstTruyen; i++){
				TruyenTinhCam truyen = lstTruyen.get(i);
				String linkSource = truyen.getLinkSource();
				try {
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345732336.1345745594.5; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=32; __utmc=202170061; cdt_max_clicks=3; __utmb=202170061.18.10.1345745594; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%2245f20e3a00a1c0e720ccc52c9684a3f3%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345745613%22%3B%7Dc252b482853d6d9a8cf810a96beae85b")
						    .header("Host", "mtruyen.hixx.info")
						    .header("Referer", "http://truyen.hixx.info/truyen/truyen-tinh-cam/tat-ca.html")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element idTruyenTranhChiTiet = doc.select("#truyen_tranh_chi_tiet").first();
					String info = truyen.getInfo();
					if(info == null || info.equals("")){
						info = idTruyenTranhChiTiet.select(".desc").text().replace("<", "").replace(">", "").replace("\"", "'");
						truyen.setInfo(info.trim());
					}//ket thuc info == null || info.equals("")
					Date postingTime = truyen.getPostingTime();
					if(postingTime == null){
						postingTime = new Date();
						truyen.setPostingTime(postingTime);
					}//ket thuc postingTime == null
					Elements bt_pagination = idTruyenTranhChiTiet.select(".bt_pagination");
					//xu ly neu truyen co phan trang so chuong
					if(bt_pagination.size() > 0){
						String trang = bt_pagination.first().select("div:eq(0)").text().trim();
						int beginIndex = trang.lastIndexOf("/") + 1;
						String totalPage = trang.substring(beginIndex, trang.length()).trim();
						String hrefNext = bt_pagination.select(".next").first().select("a").first().attr("href");
						int endIndex = hrefNext.lastIndexOf("/");
						String hrefPage = hrefNext.substring(0, endIndex);
						int totalPag = Integer.parseInt(totalPage) + 1;
						for(int j = 1; j < totalPag ; j ++){
							Document docJ = Jsoup.connect(hrefPage+"/index"+j+".html")
									.timeout(1000000)
									.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
								    .header("Accept-Encoding", "gzip, deflate")
								    .header("Accept-Language", "en-us,en;q=0.5")
								    .header("Cache-Control", "max-age=0")
								    .header("Connection", "keep-alive")
								    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345732336.1345745594.5; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=47; __utmc=202170061; cdt_max_clicks=3; __utmb=202170061.48.10.1345745594; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22e74dd4fd8ff44e985fb7646aed01d956%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345750551%22%3B%7D54a39f03de3e8536923d8a1b79962d07")
								    .header("Host", "mtruyen.hixx.info")
								    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
								    .get();
							Element idTruyenTranhChiTietDocJ = docJ.select("#truyen_tranh_chi_tiet").first();
							Elements classDanhSach = idTruyenTranhChiTietDocJ.select("div.danh_sach");
							if(classDanhSach.size() > 0){
								int sizeDanhSach = classDanhSach.size();
								//bo 5 chuong dau nen dat iDanSach = 5
								for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
									Element aDanhSach = classDanhSach.get(iDanhSach).select("a").first();
									String linkSourceChapter = aDanhSach.attr("href");
									String textADanhSach = aDanhSach.text();
									int beginIndex1 = textADanhSach.indexOf("--", 2);
									String nameChapter = textADanhSach.substring(beginIndex1+2,textADanhSach.length()).trim();
									String nameChapterUnsigned = Unsigned.boDau(nameChapter);
									ChapterTmpTruyenTinhCam chapterTmpTruyenTinhCam = chapterTmpTruyenTinhCamService.findAliasvTruyen(truyen, nameChapterUnsigned);
									ChapterTam chapterTam = chapterTamService.findAliasvTruyen(truyen,nameChapterUnsigned);
									if(chapterTmpTruyenTinhCam == null && chapterTam==null){
										String chapterN = textADanhSach.substring(0,beginIndex1).replace("-", "").trim();
										int chapterNumber = Integer.parseInt(chapterN);
										chapterTam = new ChapterTam(truyen,nameChapter,nameChapterUnsigned,linkSourceChapter,chapterNumber,2);
										chapterTamService.save(chapterTam);
									}
								}//ket thuc vong lap for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
							}//ket thuc if classDanhSach.size() > 0
						}//ket thuc vong lap for(int j = 1; j < totalPag ; j ++){
					}else{
						Elements classDanhSach = idTruyenTranhChiTiet.select("div.danh_sach");
						if(classDanhSach.size() > 0){
							int sizeDanhSach = classDanhSach.size();
							for(int iDanhSach = 0; iDanhSach < sizeDanhSach; iDanhSach++){
								Element aDanhSach = classDanhSach.get(iDanhSach).select("a").first();
								String linkSourceChapter = aDanhSach.attr("href");
								String textADanhSach = aDanhSach.text();
								int beginIndex1 = textADanhSach.indexOf("--", 2);
								String nameChapter = textADanhSach.substring(beginIndex1+2,textADanhSach.length()).trim();
								String nameChapterUnsigned = Unsigned.boDau(nameChapter);
								ChapterTmpTruyenTinhCam chapterTmpTruyenTinhCam = chapterTmpTruyenTinhCamService.findAliasvTruyen(truyen, nameChapterUnsigned);
								ChapterTam chapterTam = chapterTamService.findAliasvTruyen(truyen,nameChapterUnsigned);
								if(chapterTmpTruyenTinhCam == null && chapterTam==null){
									String chapterN = textADanhSach.substring(0,beginIndex1).replace("-", "").trim();
									int chapterNumber = Integer.parseInt(chapterN);
									chapterTam = new ChapterTam(truyen,nameChapter,nameChapterUnsigned,linkSourceChapter,chapterNumber,2);
									chapterTamService.save(chapterTam);
								}
							}//ket thuc vong lap for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
						}//ket thuc if classDanhSach.size() > 0
					}//ket thuc xu ly neu truyen co phan trang so chuong if(bt_pagination.size() > 0){
					String linkImage = truyen.getLinkImage();
					if(linkImage == null){
						Element divClassImage = idTruyenTranhChiTiet.select(".image").first().select("img").first();
						if(divClassImage != null){
							String image = divClassImage.attr("src");
							if(image.equals("http://truyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg") || image.equals("http://mtruyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg")){
								truyen.setLinkImage("http://1.bp.blogspot.com/-4oYsOZRo7Ro/UElc1Si1o1I/AAAAAAAAAO4/GkoWJeYohQo");
								truyen.setNameImage(truyen.getAlias().replace(" ", "-")+".jpg");
							}else{
								image = image.replace(" ", "%20");
								URL fileURL = new URL(image);
								URLConnection urlConn = fileURL.openConnection();
							    urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)");
								InputStream inputStream = urlConn.getInputStream();
								String nameNewFileImage = "truyen-"+truyen.getAlias().replace(" ", "-");
								byte[] imageBytes = IOUtils.toByteArray(inputStream);
								//khai bao url album picasa dua theo user va albumId
								URL albumPostUrlComic = new URL("https://picasaweb.google.com/data/feed/api/user/110519699166210558008/albumid/5803577941033961393");
								PhotoEntry myPhotoComic = new PhotoEntry();
								//dat ten cho tam anh khi up len picasa
								myPhotoComic.setTitle(new PlainTextConstruct(nameNewFileImage));
								//chuyen doi anh thanh .jpg
								MediaByteArraySource myMediaComic = new MediaByteArraySource(imageBytes, "image/jpeg");
								myPhotoComic.setMediaSource(myMediaComic);
								//tien hanh upload anh len picasa
								PhotoEntry returnedPhoto = myService.insert(albumPostUrlComic, myPhotoComic);
								String newSrc = returnedPhoto.getHtmlLink().getHref();
								if (returnedPhoto.getMediaContents().size() > 0) {
									newSrc = returnedPhoto.getMediaContents().get(0).getUrl();
									int endNewSrc = newSrc.lastIndexOf("/");
									String urlGooglePicasa = newSrc.substring(0, endNewSrc);
									urlGooglePicasa = urlGooglePicasa.replace("https://lh3.googleusercontent.com", "http://1.bp.blogspot.com")
									.replace("https://lh4.googleusercontent.com", "http://2.bp.blogspot.com")
									.replace("https://lh5.googleusercontent.com", "http://3.bp.blogspot.com")
									.replace("https://lh6.googleusercontent.com", "http://4.bp.blogspot.com")
									.replace("lh3.ggpht.com", "1.bp.blogspot.com")
									.replace("lh4.ggpht.com", "2.bp.blogspot.com")
									.replace("lh5.ggpht.com", "3.bp.blogspot.com")
									.replace("lh6.ggpht.com", "4.bp.blogspot.com");
									truyen.setLinkImage(urlGooglePicasa);
									truyen.setNameImage(nameNewFileImage);
								}//ket thuc if (returnedPhoto.getMediaContents().size() > 0) {
							}//ket thuc if(image.equals("http://truyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg")){
						}//ket thuc divClassImage != null
					}//ket thu linkImage == null
					truyenTinhCamDao.merge(truyen);
//					logJsoupService.writeLog("OK - Hixx.info - Cập nhật truyện "+truyen.getName(),"Cập nhật truyện "+truyen.getName()+" thành công",loaiLog,false);
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - Hixx.info - Lỗi cập nhật truyện tình cảm"+truyen.getName(),"LinkSource "+linkSource,loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc vong lap for(int i = 0; i < sizeLstTruyen; i++){
			logJsoupService.writeLog("OK - Hixx.info - Cập nhật truyện","Đã cập nhật xong truyện tình cảm",loaiLog,false);
		}//end if(lstTruyen.size() > 0){
		getContentChapterTruyenTinhCamFromHixx();
	}//ket thuc ham

	//lay noi dung cua chuong
	@Async
	@Override
	public void getContentChapterTruyenTinhCamFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		int sourceBy = 2;
		List<ChapterTam> lstChapterTam = chapterTamService.getLstFromSourceByVTruyenNotNull(sourceBy,"tinhCam");
		if(lstChapterTam.size() > 0){
			int sizeLstChapterTam = lstChapterTam.size();
			for(int i = 0; i < sizeLstChapterTam; i++){
				ChapterTam chapterTam = lstChapterTam.get(i);
				String nameChapterTruyen = chapterTam.getName();
				String nameChapterUnsigned = chapterTam.getAlias();
				TruyenTinhCam truyen = chapterTam.getTruyenTinhCam();
				String linkSource = chapterTam.getLinkSource();
				String content = "";
				try {
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345832917.1345860864.9; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=10; cdt_max_clicks=4; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22bec81191a5c86ae3766cfd8451a9e905%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345860865%22%3B%7D93bee1ea31195046620d2e6ec395b87b; __utmb=202170061.6.10.1345860864; __utmc=202170061")
						    .header("Host", "mtruyen.hixx.info")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element idNoiDungChiTiet = doc.select("#ar-content-html").first();
					idNoiDungChiTiet.select("div[style^=float:right; padding: 10px]").remove();
					idNoiDungChiTiet.select("script").remove();
					idNoiDungChiTiet.select("noscript").remove();
					content = idNoiDungChiTiet.html();
					if(!content.equals("")){
						float chapterNumber = chapterTam.getChapterNumber();
						Date postingTime = new Date();
						ChapterTruyenTinhCam chapterTruyen = new ChapterTruyenTinhCam(nameChapterTruyen,nameChapterUnsigned,chapterNumber,content,postingTime,truyen);
						chapterTruyenTinhCamDao.save(chapterTruyen);
						ChapterTmpTruyenTinhCam chapterTmpTruyenTinhCam = new ChapterTmpTruyenTinhCam(chapterTruyen.getId(),nameChapterTruyen,nameChapterUnsigned,chapterNumber,truyen);
						chapterTmpTruyenTinhCamDao.save(chapterTmpTruyenTinhCam);
						chapterTamService.delete(chapterTam.getId());
//						logJsoupService.writeLog("OK - Hixx.info - Cập nhật xong chương  "+nameChapterTruyen,"Đã lấy xong nội dung "+nameChapterTruyen +" của truyện "+truyen.getName(),loaiLog,false);
					}//ket thuc if(!content.equals("")){
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - Hixx.info - Lỗi cập nhật nội dung truyện "+nameChapterTruyen,"LinkSource "+linkSource,loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc if(lstChapterTam.size() > 0){
		}//ket thuc vong lap for(int i = 0; i < sizeLstChapterTam; i++){
		logJsoupService.writeLog("OK - Hixx.info","Đã lấy xong nội dung của các chương truyện tình cảm",loaiLog,false);
	}//ket thuc ham
	
	/*************************************************************
	 * 
	 * lay truyen trinh tham tu hixx.info
	 * 
	**************************************************************/
	
	//lay ten truyen trinh tham,ten tac gia
	@Async
	@Override
	public void getLinkSourceTruyenTrinhThamFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			Document doc = Jsoup.connect("http://truyen.hixx.info/truyen/truyen-trinh-tham/tat-ca.html")
					.timeout(1000000)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
				    .header("Accept-Encoding", "gzip, deflate")
				    .header("Accept-Language", "en-us,en;q=0.5")
				    .header("Cache-Control", "max-age=0")
				    .header("Connection", "keep-alive")
				    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmb=233845639.1.10.1345655004; __utmc=233845639; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345655010.1345655010.1; __utmb=202170061.6.10.1345655010; __utmc=202170061; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=3; cdt_max_clicks=4")
				    .header("Host", "mtruyen.hixx.info")
				    .header("Referer", "http://truyen.hixx.info/truyen/truyen-trinh-tham/tat-ca.html")
				    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
				    .get();
			Elements li = doc.select("#content_left div.bg ul.top li");
			if(li.size() > 0){
				int sizeList = li.size();
				for(int i = 0; i < sizeList ; i++){
					Element item = li.get(i);
					Element aStory = item.select("a.story").first();
					String nameTruyen = aStory.text();
					String alias = Unsigned.boDau(nameTruyen);
					TruyenTrinhTham truyen = truyenTrinhThamService.findByProperty("alias", alias);
					Element spanChapterClear = item.select("span.chapter").first();
					String maxChapter = spanChapterClear.select("a.story").text().replace("-", "").replace(" ", "").trim();
					float totalChapter = Float.parseFloat(maxChapter);
					String finished = spanChapterClear.text();
					int beginIndex = finished.lastIndexOf("|")+1;
					finished = finished.substring(beginIndex, finished.length()).trim();
					boolean isFinished = false;
					if(finished.equals("Đủ bộ")){
						isFinished = true;
					}//ket thuc if(finished.equals("Đủ bộ")){
					if(truyen == null){
						String linkSource = aStory.attr("href");
						linkSource = linkSource.replace(" ", "%20");
						Element aLast = item.select("a").last();
						String nameAuthor = aLast.text();
						TacGia tacGia = tacGiaService.findByAliasLoaiTruyen("unknown", 4);
						if(nameAuthor != null && !nameAuthor.equals("")){
							String authorUnsigned = Unsigned.boDau(nameAuthor);
							tacGia = tacGiaService.findByAliasLoaiTruyen(authorUnsigned, 4);
							if(tacGia == null){
								tacGia = new TacGia(nameAuthor,authorUnsigned, 4);
								tacGiaService.save(tacGia);
							}
						}
						int soureBy = 2;
						Account account = userService.findById((long) 1);
						truyen = new TruyenTrinhTham(linkSource,nameTruyen,alias,soureBy,totalChapter,tacGia,isFinished,account);
						truyenTrinhThamService.save(truyen);
					}else{
						if(!truyen.getIsFinished()){
							truyen.setTotalChapter(totalChapter);
							truyen.setIsFinished(isFinished);
							truyenTrinhThamDao.merge(truyen);
						}
					}//ket thuc if(truyen == null){
//					logJsoupService.writeLog("OK - hixxinfo - Lấy truyện theo tất cả","Đã lấy xong truyện "+nameTruyen,loaiLog,false);
				}//ket thuc for(int i = 0; i < sizeList ; i++){
			}//ket thuc if(li.size() > 0){
			logJsoupService.writeLog("OK - hixxinfo - Lấy truyện","Đã lấy xong truyện trinh thám",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - hixxinfo  - Lấy truyện","Có lỗi xảy ra khi lấy truyện trinh thám",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
		getImageInfoChapterTruyenTrinhThamFromHixx();
	}

	//lay hinh anh va danh sach chuong
	@Async
	@Override
	public void getImageInfoChapterTruyenTrinhThamFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		PicasawebService myService = new PicasawebService("zaza");
		try {
			myService.setUserCredentials("truyenzcom1@gmail.com", "11062010");
		} catch (AuthenticationException e1) {
			logJsoupService.writeLog("ERROR - Kết nối với picasa","kết nối picasa thất bại",loaiLog,true);
			e1.printStackTrace();
		}//end try {
		List<TruyenTrinhTham> lstTruyen = truyenTrinhThamService.getLstTruyenBySourceBy(2);
//			List<Truyen> lstTruyen = truyenTrinhThamService.getLstComicLinkImageNullBlogTruyen("hixxinfo");
		if(lstTruyen.size() > 0){
			int sizeLstTruyen = lstTruyen.size();
			for(int i = 0; i < sizeLstTruyen; i++){
				TruyenTrinhTham truyen = lstTruyen.get(i);
				String linkSource = truyen.getLinkSource();
				try {
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345732336.1345745594.5; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=32; __utmc=202170061; cdt_max_clicks=3; __utmb=202170061.18.10.1345745594; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%2245f20e3a00a1c0e720ccc52c9684a3f3%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345745613%22%3B%7Dc252b482853d6d9a8cf810a96beae85b")
						    .header("Host", "mtruyen.hixx.info")
						    .header("Referer", "http://truyen.hixx.info/truyen/truyen-trinh-tham/tat-ca.html")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element idTruyenTranhChiTiet = doc.select("#truyen_tranh_chi_tiet").first();
					String info = truyen.getInfo();
					if(info == null || info.equals("")){
						info = idTruyenTranhChiTiet.select(".desc").text().replace("<", "").replace(">", "").replace("\"", "'");
						truyen.setInfo(info.trim());
					}//ket thuc info == null || info.equals("")
					Date postingTime = truyen.getPostingTime();
					if(postingTime == null){
						postingTime = new Date();
						truyen.setPostingTime(postingTime);
					}//ket thuc postingTime == null
					Elements bt_pagination = idTruyenTranhChiTiet.select(".bt_pagination");
					//xu ly neu truyen co phan trang so chuong
					if(bt_pagination.size() > 0){
						String trang = bt_pagination.first().select("div:eq(0)").text().trim();
						int beginIndex = trang.lastIndexOf("/") + 1;
						String totalPage = trang.substring(beginIndex, trang.length()).trim();
						String hrefNext = bt_pagination.select(".next").first().select("a").first().attr("href");
						int endIndex = hrefNext.lastIndexOf("/");
						String hrefPage = hrefNext.substring(0, endIndex);
						int totalPag = Integer.parseInt(totalPage) + 1;
						for(int j = 1; j < totalPag ; j ++){
							Document docJ = Jsoup.connect(hrefPage+"/index"+j+".html")
									.timeout(1000000)
									.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
								    .header("Accept-Encoding", "gzip, deflate")
								    .header("Accept-Language", "en-us,en;q=0.5")
								    .header("Cache-Control", "max-age=0")
								    .header("Connection", "keep-alive")
								    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345732336.1345745594.5; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=47; __utmc=202170061; cdt_max_clicks=3; __utmb=202170061.48.10.1345745594; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22e74dd4fd8ff44e985fb7646aed01d956%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345750551%22%3B%7D54a39f03de3e8536923d8a1b79962d07")
								    .header("Host", "mtruyen.hixx.info")
								    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
								    .get();
							Element idTruyenTranhChiTietDocJ = docJ.select("#truyen_tranh_chi_tiet").first();
							Elements classDanhSach = idTruyenTranhChiTietDocJ.select("div.danh_sach");
							if(classDanhSach.size() > 0){
								int sizeDanhSach = classDanhSach.size();
								//bo 5 chuong dau nen dat iDanSach = 5
								for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
									Element aDanhSach = classDanhSach.get(iDanhSach).select("a").first();
									String linkSourceChapter = aDanhSach.attr("href");
									String textADanhSach = aDanhSach.text();
									int beginIndex1 = textADanhSach.indexOf("--", 2);
									String nameChapter = textADanhSach.substring(beginIndex1+2,textADanhSach.length()).trim();
									String nameChapterUnsigned = Unsigned.boDau(nameChapter);
									ChapterTmpTruyenTrinhTham chapterTmpTruyenTrinhTham = chapterTmpTruyenTrinhThamService.findAliasvTruyen(truyen, nameChapterUnsigned);
									ChapterTam chapterTam = chapterTamService.findAliasvTruyen(truyen,nameChapterUnsigned);
									if(chapterTmpTruyenTrinhTham == null && chapterTam==null){
										String chapterN = textADanhSach.substring(0,beginIndex1).replace("-", "").trim();
										int chapterNumber = Integer.parseInt(chapterN);
										chapterTam = new ChapterTam(truyen,nameChapter,nameChapterUnsigned,linkSourceChapter,chapterNumber,2);
										chapterTamService.save(chapterTam);
									}
								}//ket thuc vong lap for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
							}//ket thuc if classDanhSach.size() > 0
						}//ket thuc vong lap for(int j = 1; j < totalPag ; j ++){
					}else{
						Elements classDanhSach = idTruyenTranhChiTiet.select("div.danh_sach");
						if(classDanhSach.size() > 0){
							int sizeDanhSach = classDanhSach.size();
							for(int iDanhSach = 0; iDanhSach < sizeDanhSach; iDanhSach++){
								Element aDanhSach = classDanhSach.get(iDanhSach).select("a").first();
								String linkSourceChapter = aDanhSach.attr("href");
								String textADanhSach = aDanhSach.text();
								int beginIndex1 = textADanhSach.indexOf("--", 2);
								String nameChapter = textADanhSach.substring(beginIndex1+2,textADanhSach.length()).trim();
								String nameChapterUnsigned = Unsigned.boDau(nameChapter);
								ChapterTmpTruyenTrinhTham chapterTmpTruyenTrinhTham = chapterTmpTruyenTrinhThamService.findAliasvTruyen(truyen, nameChapterUnsigned);
								ChapterTam chapterTam = chapterTamService.findAliasvTruyen(truyen,nameChapterUnsigned);
								if(chapterTmpTruyenTrinhTham == null && chapterTam==null){
									String chapterN = textADanhSach.substring(0,beginIndex1).replace("-", "").trim();
									int chapterNumber = Integer.parseInt(chapterN);
									chapterTam = new ChapterTam(truyen,nameChapter,nameChapterUnsigned,linkSourceChapter,chapterNumber,2);
									chapterTamService.save(chapterTam);
								}
							}//ket thuc vong lap for(int iDanhSach = 5; iDanhSach < sizeDanhSach; iDanhSach++){
						}//ket thuc if classDanhSach.size() > 0
					}//ket thuc xu ly neu truyen co phan trang so chuong if(bt_pagination.size() > 0){
					String linkImage = truyen.getLinkImage();
					if(linkImage == null){
						Element divClassImage = idTruyenTranhChiTiet.select(".image").first().select("img").first();
						if(divClassImage != null){
							String image = divClassImage.attr("src");
							if(image.equals("http://truyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg") || image.equals("http://mtruyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg")){
								truyen.setLinkImage("http://1.bp.blogspot.com/-4oYsOZRo7Ro/UElc1Si1o1I/AAAAAAAAAO4/GkoWJeYohQo");
								truyen.setNameImage(truyen.getAlias().replace(" ", "-")+".jpg");
							}else{
								image = image.replace(" ", "%20");
								URL fileURL = new URL(image);
								URLConnection urlConn = fileURL.openConnection();
							    urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)");
								InputStream inputStream = urlConn.getInputStream();
								String nameNewFileImage = "truyen-"+truyen.getAlias().replace(" ", "-");
								byte[] imageBytes = IOUtils.toByteArray(inputStream);
								//khai bao url album picasa dua theo user va albumId
								URL albumPostUrlComic = new URL("https://picasaweb.google.com/data/feed/api/user/110519699166210558008/albumid/5803577826525426385");
								PhotoEntry myPhotoComic = new PhotoEntry();
								//dat ten cho tam anh khi up len picasa
								myPhotoComic.setTitle(new PlainTextConstruct(nameNewFileImage));
								//chuyen doi anh thanh .jpg
								MediaByteArraySource myMediaComic = new MediaByteArraySource(imageBytes, "image/jpeg");
								myPhotoComic.setMediaSource(myMediaComic);
								//tien hanh upload anh len picasa
								PhotoEntry returnedPhoto = myService.insert(albumPostUrlComic, myPhotoComic);
								String newSrc = returnedPhoto.getHtmlLink().getHref();
								if (returnedPhoto.getMediaContents().size() > 0) {
									newSrc = returnedPhoto.getMediaContents().get(0).getUrl();
									int endNewSrc = newSrc.lastIndexOf("/");
									String urlGooglePicasa = newSrc.substring(0, endNewSrc);
									urlGooglePicasa = urlGooglePicasa.replace("https://lh3.googleusercontent.com", "http://1.bp.blogspot.com")
									.replace("https://lh4.googleusercontent.com", "http://2.bp.blogspot.com")
									.replace("https://lh5.googleusercontent.com", "http://3.bp.blogspot.com")
									.replace("https://lh6.googleusercontent.com", "http://4.bp.blogspot.com")
									.replace("lh3.ggpht.com", "1.bp.blogspot.com")
									.replace("lh4.ggpht.com", "2.bp.blogspot.com")
									.replace("lh5.ggpht.com", "3.bp.blogspot.com")
									.replace("lh6.ggpht.com", "4.bp.blogspot.com");
									truyen.setLinkImage(urlGooglePicasa);
									truyen.setNameImage(nameNewFileImage);
								}//ket thuc if (returnedPhoto.getMediaContents().size() > 0) {
							}//ket thuc if(image.equals("http://truyen.hixx.info/assets/truyen/images/no_image_found_120x180.jpg")){
						}//ket thuc divClassImage != null
					}//ket thu linkImage == null
					truyenTrinhThamDao.merge(truyen);
//					logJsoupService.writeLog("OK - Hixx.info - Cập nhật truyện "+truyen.getName(),"Cập nhật truyện "+truyen.getName()+" thành công",loaiLog,false);
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - Hixx.info - Lỗi cập nhật truyện trinh thám"+truyen.getName(),"LinkSource "+linkSource,loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc vong lap for(int i = 0; i < sizeLstTruyen; i++){
			logJsoupService.writeLog("OK - Hixx.info - Cập nhật truyện","Đã cập nhật xong truyện trinh thám",loaiLog,false);
		}//end if(lstTruyen.size() > 0){
		getContentChapterTruyenTrinhThamFromHixx();
	}//ket thuc ham

	//lay noi dung cua chuong
	@Async
	@Override
	public void getContentChapterTruyenTrinhThamFromHixx() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		int sourceBy = 2;
		List<ChapterTam> lstChapterTam = chapterTamService.getLstFromSourceByVTruyenNotNull(sourceBy,"trinhTham");
		if(lstChapterTam.size() > 0){
			int sizeLstChapterTam = lstChapterTam.size();
			for(int i = 0; i < sizeLstChapterTam; i++){
				ChapterTam chapterTam = lstChapterTam.get(i);
				String nameChapterTruyen = chapterTam.getName();
				String nameChapterUnsigned = chapterTam.getAlias();
				TruyenTrinhTham truyen = chapterTam.getTruyenTrinhTham();
				String linkSource = chapterTam.getLinkSource();
				String content = "";
				try {
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=233845639.1212711466.1345655004.1345655004.1345655004.1; __utmz=233845639.1345655004.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=202170061.1134629760.1345655010.1345832917.1345860864.9; __utmz=202170061.1345655010.1.1.utmcsr=truyen.hixx.info|utmccn=(referral)|utmcmd=referral|utmcct=/truyen.html; __gads=ID=d7d8fbaf954644b6:T=1345655011:S=ALNI_MYh22YLd8Eu0qy-FqIa4SeDvRzxLw; cdt_popup_count=10; cdt_max_clicks=4; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22bec81191a5c86ae3766cfd8451a9e905%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A13%3A%22118.69.55.161%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A50%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%3B+rv%3A14.0%29+Gecko%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221345860865%22%3B%7D93bee1ea31195046620d2e6ec395b87b; __utmb=202170061.6.10.1345860864; __utmc=202170061")
						    .header("Host", "mtruyen.hixx.info")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element idNoiDungChiTiet = doc.select("#ar-content-html").first();
					idNoiDungChiTiet.select("div[style^=float:right; padding: 10px]").remove();
					idNoiDungChiTiet.select("script").remove();
					idNoiDungChiTiet.select("noscript").remove();
					content = idNoiDungChiTiet.html();
					if(!content.equals("")){
						float chapterNumber = chapterTam.getChapterNumber();
						Date postingTime = new Date();
						ChapterTruyenTrinhTham chapterTruyen = new ChapterTruyenTrinhTham(nameChapterTruyen,nameChapterUnsigned,chapterNumber,content,postingTime,truyen);
						chapterTruyenTrinhThamDao.save(chapterTruyen);
						ChapterTmpTruyenTrinhTham chapterTmpTruyenTrinhTham = new ChapterTmpTruyenTrinhTham(chapterTruyen.getId(),nameChapterTruyen,nameChapterUnsigned,chapterNumber,truyen);
						chapterTmpTruyenTrinhThamDao.save(chapterTmpTruyenTrinhTham);
						chapterTamService.delete(chapterTam.getId());
//						logJsoupService.writeLog("OK - Hixx.info - Cập nhật xong chương  "+nameChapterTruyen,"Đã lấy xong nội dung "+nameChapterTruyen +" của truyện "+truyen.getName(),loaiLog,false);
					}//ket thuc if(!content.equals("")){
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - Hixx.info - Lỗi cập nhật nội dung truyện "+nameChapterTruyen,"LinkSource "+linkSource,loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//ket thuc if(lstChapterTam.size() > 0){
		}//ket thuc vong lap for(int i = 0; i < sizeLstChapterTam; i++){
		logJsoupService.writeLog("OK - Hixx.info","Đã lấy xong nội dung của các chương trinh thám",loaiLog,false);
	}//ket thuc ham

	/*************************************************************
	 * 
	 * lay truyen ma tu yeutruyen.vn
	 * 
	**************************************************************/
	@Async
	@Override
	public void getLinkSourceTruyenMa() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup thegioima");
		int soureBy = 5;
		Account account = userService.findById((long) 1);
		TacGia tacGia = tacGiaService.findById((long) 5);
		String linkImage = "http://1.bp.blogspot.com/-4oYsOZRo7Ro/UElc1Si1o1I/AAAAAAAAAO4/GkoWJeYohQo";
		PicasawebService myService = new PicasawebService("zaza");
		try {
			myService.setUserCredentials("truyenzcom1@gmail.com", "11062010");
		} catch (AuthenticationException e1) {
			logJsoupService.writeLog("ERROR - Kết nối với picasa","kết nối picasa thất bại",loaiLog,true);
			e1.printStackTrace();
		}//end try {
		for(int i = 1; i < 3; i++){
			try {
				String url = "http://yeutruyen.vn/category/truyen-ma/page-"+i;
				Document doc = Jsoup.connect(url)
						.timeout(1000000)
						.header("Accept", "*/*")
					    .header("Accept-Encoding", "gzip, deflate")
					    .header("Accept-Language", "en-us,en;q=0.5")
					    .header("Cache-Control", "max-age=0")
					    .header("Connection", "keep-alive")
					    .header("Cookie", "PHPSESSID=c1ebc6759a9238accbb9b18bdf5570d1")
					    .header("Host", "yeutruyen.vn")
					    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
					    .get();
				Elements divMenu = doc.select("#content .post-item");
				int sizeMenu = divMenu.size();
				for(int j = 0 ; j < sizeMenu; j ++){
					Element divItem = divMenu.get(j);
					Element a = divItem.select("a").first();
					String href= a.attr("href");
					String name = a.text().replace("\"", "");
					String alias = Unsigned.boDau(name);
					TruyenMa truyen = truyenMaService.findByProperty("alias", alias);
					if(truyen==null){
						String nameImage = alias.replace(" ", "-")+".jpg";
						Element divClassImage = divItem.select("img").first();
						if(divClassImage != null){
							try {
								String image = divClassImage.attr("src");
								image = image.replace(" ", "%20");
								URL fileURL = new URL(image);
								URLConnection urlConn = fileURL.openConnection();
							    urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)");
								InputStream inputStream = urlConn.getInputStream();
								byte[] imageBytes = IOUtils.toByteArray(inputStream);
								//khai bao url album picasa dua theo user va albumId
								URL albumPostUrlComic = new URL("https://picasaweb.google.com/data/feed/api/user/110519699166210558008/albumid/5808491925157857345");
								PhotoEntry myPhotoComic = new PhotoEntry();
								//dat ten cho tam anh khi up len picasa
								myPhotoComic.setTitle(new PlainTextConstruct(alias));
								//chuyen doi anh thanh .jpg
								MediaByteArraySource myMediaComic = new MediaByteArraySource(imageBytes, "image/jpeg");
								myPhotoComic.setMediaSource(myMediaComic);
								//tien hanh upload anh len picasa
								PhotoEntry returnedPhoto = myService.insert(albumPostUrlComic, myPhotoComic);
								String newSrc = returnedPhoto.getHtmlLink().getHref();
								if (returnedPhoto.getMediaContents().size() > 0) {
									newSrc = returnedPhoto.getMediaContents().get(0).getUrl();
									int endNewSrc = newSrc.lastIndexOf("/");
									String urlGooglePicasa = newSrc.substring(0, endNewSrc);
									urlGooglePicasa = urlGooglePicasa.replace("https://lh3.googleusercontent.com", "http://1.bp.blogspot.com")
									.replace("https://lh4.googleusercontent.com", "http://2.bp.blogspot.com")
									.replace("https://lh5.googleusercontent.com", "http://3.bp.blogspot.com")
									.replace("https://lh6.googleusercontent.com", "http://4.bp.blogspot.com")
									.replace("lh3.ggpht.com", "1.bp.blogspot.com")
									.replace("lh4.ggpht.com", "2.bp.blogspot.com")
									.replace("lh5.ggpht.com", "3.bp.blogspot.com")
									.replace("lh6.ggpht.com", "4.bp.blogspot.com");
									linkImage = urlGooglePicasa;
								}//ket thuc if (returnedPhoto.getMediaContents().size() > 0) {
							} catch (Exception e) {
								e.printStackTrace();
							}//end try {
						}//ket thuc divClassImage != null
						truyen = new TruyenMa(href,name,alias,soureBy,account,tacGia,linkImage,nameImage,new Date());
						truyenMaService.save(truyen);
					}//ket thuc if(comic==null){
				}//end for(int j = 0 ; j < sizeMenu; j ++){
			}catch (Exception e) {
				logJsoupService.writeLog("ERROR - yeutruyen.vn","Lấy truyện ma bị lỗi",loaiLog,true);
				e.printStackTrace();
			}//ket thuc try catch
		}
	}

	@Async
	@Override
	public void getContentChapterTruyenMa() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup thegioima");
		List<TruyenMa> lstTruyen = truyenMaService.getLstTruyenBySourceBy(5);
		if(lstTruyen.size() > 0){
			int sizeLstTruyen = lstTruyen.size();
			for(int i = 0; i < sizeLstTruyen; i++){
				TruyenMa truyen = lstTruyen.get(i);
				String linkSource = truyen.getLinkSource();
				try {
					Document doc = Jsoup.connect("http://yeutruyen.vn"+linkSource)
							.timeout(1000000)
							.header("Accept", "*/*")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "PHPSESSID=c1ebc6759a9238accbb9b18bdf5570d1")
						    .header("Host", "yeutruyen.vn")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Elements page = doc.select(".page");
					int sizePage = 1;
					int endIndex = linkSource.lastIndexOf(".html");
					linkSource = linkSource.substring(0, endIndex);
					if(page.size() > 0){
						Element divPage=page.first();
						Elements aPage = divPage.select("a");
						Element aLastPage = aPage.last().previousElementSibling();
						sizePage = Integer.parseInt(aLastPage.text()) + 1;
					}//end if(divPage != null){
					if(sizePage > 1){
						for(int ii=1; ii < sizePage; ii++){
							String linkChapter = linkSource + ".part" + ii;
							try {
								Document doc1 = Jsoup.connect("http://yeutruyen.vn"+linkChapter)
										.timeout(1000000)
										.header("Accept", "*/*")
									    .header("Accept-Encoding", "gzip, deflate")
									    .header("Accept-Language", "en-us,en;q=0.5")
									    .header("Cache-Control", "max-age=0")
									    .header("Connection", "keep-alive")
									    .header("Cookie", "PHPSESSID=c1ebc6759a9238accbb9b18bdf5570d1")
									    .header("Host", "yeutruyen.vn")
									    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
									    .get();
								Element idNoiDungChiTiet = doc1.select("#content .entry").first();
								idNoiDungChiTiet.select("script").remove();
								idNoiDungChiTiet.select("noscript").remove();
								String content = idNoiDungChiTiet.html();
								if(!content.equals("")){
									float chapterNumber = ii;
									Date postingTime = new Date();
									String nameChapterTruyen = "Chương "+ ii;
									String nameChapterUnsigned = Unsigned.boDau(nameChapterTruyen);
									ChapterTruyenMa chapterTruyen = chapterTruyenMaService.findAliasvTruyen(truyen, nameChapterUnsigned);
									if(chapterTruyen == null){
										chapterTruyen = new ChapterTruyenMa(nameChapterTruyen,nameChapterUnsigned,chapterNumber,content,postingTime,truyen);
										chapterTruyenMaDao.save(chapterTruyen);
										ChapterTmpTruyenMa chapterTmpTruyenMa = new ChapterTmpTruyenMa(chapterTruyen.getId(),nameChapterTruyen,nameChapterUnsigned,chapterNumber,truyen);
										chapterTmpTruyenMaDao.save(chapterTmpTruyenMa);
									}
								}//ket thuc if(!content.equals("")){
							} catch (Exception e) {
								logJsoupService.writeLog("ERROR - yeutruyen.vn","Lấy chapter truyện ma "+linkChapter,loaiLog,true);
								e.printStackTrace();
							}
						}//end for(int ii=1; ii < sizePage; ii++){
					}//end if(sizePage > 1){
					if(sizePage > 1){
						sizePage = sizePage - 1;
					}
					truyen.setTotalChapter(sizePage);
					truyenMaDao.merge(truyen);
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - yeutruyen.vn","Lấy chapter truyện ma "+truyen.getName(),loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//end for(int i = 0; i < sizeLstTruyen; i++){
		}//end if(lstTruyen.size() > 0){
	}//ket thuc ham
	
	/*************************************************************
	 * 
	 * lay anh dep tu photo.hoanvu.net
	 * 
	 *************************************************************/
	
	@Async
	@Override
	public void getLinkAlbum() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup anh dep");
		PicasawebService myService = null;
		try {
			myService = new PicasawebService("zaza");
			myService.setUserCredentials("anhsexytruyenzcom@gmail.com", "11062010");
			try {
				for(int j=1; j<6;j++){
					Document doc = Jsoup.connect("http://photo.hoanvu.net/?action=ajax_post&pag="+j)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
						    .header("Cookie", "__utma=73433995.1206855714.1352439684.1352439684.1352439684.1; __utmz=73433995.1352439684.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=152693880.1470244733.1352439694.1352445068.1352520206.3; __utmz=152693880.1352445068.2.2.utmcsr=backlink.hoanvu.net|utmccn=(referral)|utmcmd=referral|utmcct=/")
						    .header("Host", "photo.hoanvu.net")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Elements divItems = doc.select(".post-thumbnail");
					if(divItems.size() > 0){
						int sizeItem = divItems.size();
						for(int i = 0; i < sizeItem; i++){
							try{
								Element divItem = divItems.get(i);
								Element aFirst = divItem.select("a").first();
								String title = aFirst.attr("title");
								String alias = Unsigned.boDau(title);
								String href = aFirst.attr("href");
								String removeDomain = href.replace("http://photo.hoanvu.net/", "");
								int endIndex = removeDomain.indexOf("/");
								String category = removeDomain.substring(0,endIndex);
								String type = "Other";
								if(category.equals("wallpaper")){
									type = "Wallpaper";
								}else if(category.equals("tonghop")){
									type = "Tổng Hợp";
								}
								else if(category.equals("one-piece")){
									type = "One Piece";
								}else if(category.equals("japan-korea")){
									type = "Japan – Korea";
								}else if(category.equals("girl-xinh-viet-nam")){
									type = "Girl Xinh VN";
								}else if(category.equals("girl-xinh")){
									type = "Girl Xinh";
								}else if(category.equals("hot-girl-sexy")){
									type = "Girl Sexy";
								}else if(category.equals("cosplay-2")){
									type = "Cosplay";
								}else if(category.equals("chan-dai-va-xe")){
									type = "Chân Dài và Siêu Xe";
								}else if(category.equals("bikini-girl")){
									type = "Bikini Girl";
								}else{
									type = "Other";
								}
								String aliasLoaiAnh =  category.replace("-", " ");
								LoaiAnhDep loaiAnhDep = loaiAnhDepService.findByProperty("alias", aliasLoaiAnh);
								if(loaiAnhDep == null){
									loaiAnhDep = new LoaiAnhDep(type,aliasLoaiAnh);
									loaiAnhDepService.save(loaiAnhDep);
								}
								AnhDep anhDep = anhDepService.findByProperty("alias", alias);
								if(anhDep == null){
									String thumb = aFirst.select("img").attr("src");
									String removePHP = thumb.replace("http://photo.hoanvu.net/wp-content/themes/iphoto/timthumb.php?src=", "");
									int endIndex1 = removePHP.indexOf("&");
									if(endIndex1 > 0){
										removePHP = removePHP.substring(0,endIndex1);
									}
									String albumPostUrl = "https://picasaweb.google.com/data/feed/api/user/100656358956806762835/albumid/5811582099348779121";
									thumb = PicasawebAlbumUtil.uploadImagePicasa(removePHP,myService,albumPostUrl.toString());
									long views = 0;
									anhDep = new AnhDep(title,alias,href,thumb,alias.replace(" ", "-")+".jpg",new Date(),6,loaiAnhDep,views);
									anhDepService.save(anhDep);
								}
							} catch (Exception e1) {
								e1.printStackTrace();
							}//end try cacth
						}//end for(int i = 0; i < sizeTr; i++){
					}
				}
				logJsoupService.writeLog("OK - hoanvu.net","Lấy xong ảnh",loaiLog,false);
			} catch (Exception e) {
				logJsoupService.writeLog("ERROR - hoanvu.net","Lấy ảnh bị lỗi",loaiLog,true);
				e.printStackTrace();
			}//end try cacth
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - hoanvu.net","Úp ảnh lên picasa bị lỗi",loaiLog,true);
		}//end try {
		updateAlbum();
	}//end public

	@Async
	@Override
	public void updateAlbum() {
//		flagStopGetContentAnhDep = true;
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup anh dep");
		List<AnhDep> lstAll = anhDepService.getAllWithIsGot(false);
		if(lstAll.size() > 0){
			int sizeAll = lstAll.size();
			for(int i = 0; i < sizeAll; i++){
//				if(flagStopGetContentAnhDep){
					AnhDep anhDep = lstAll.get(i);
					String name2 = anhDep.getName();
					String alias2 = anhDep.getAlias();
					long idAnhDep = anhDep.getId();
					try {
						Document doc = Jsoup.connect(anhDep.getLinkSource())
								.timeout(1000000)
								.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
							    .header("Accept-Encoding", "gzip, deflate")
							    .header("Accept-Language", "en-us,en;q=0.5")
							    .header("Cache-Control", "max-age=0")
							    .header("Connection", "keep-alive")
							    .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
							    .header("Cookie", "__utma=73433995.1206855714.1352439684.1352439684.1352439684.1; __utmz=73433995.1352439684.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=152693880.1470244733.1352439694.1352445068.1352520206.3; __utmz=152693880.1352445068.2.2.utmcsr=backlink.hoanvu.net|utmccn=(referral)|utmcmd=referral|utmcct=/")
							    .header("Host", "photo.hoanvu.net")
							    .header("Referer", "http://photo.hoanvu.net/")
							    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
							    .get();
						Elements imgs = doc.select(".post-content p").first().select("img");
						if(imgs.size() > 0){
							int sizeImage = imgs.size();
							String content = "";
							ContentAnhDep cad = contentAnhDepService.findByProperty("alias", alias2);
							PicasawebService myService = null;
							try {
								myService = new PicasawebService("zaza");
								myService.setUserCredentials("anhsexytruyenzcom@gmail.com", "11062010");
							} catch (Exception e) {
								logJsoupService.writeLog("ERROR - hoanvu.net","Đăng nhập picasa bị lỗi",loaiLog,true);
							}//end try {
							if(cad == null){
								//tao album chua anh cua
								AlbumEntry myAlbum = new AlbumEntry();
								URL postUrl = new URL("http://picasaweb.google.com/data/feed/api/user/100656358956806762835");
								myAlbum.setTitle(new PlainTextConstruct(alias2));
								AlbumEntry insertedEntry = myService.insert(postUrl, myAlbum);
								String hrefAlbum = insertedEntry.getFeedLink().getHref();
								int lastHrefAlbum = hrefAlbum.lastIndexOf("?");
								String hrefAlbumFeed = hrefAlbum.substring(0, lastHrefAlbum);
								URL albumPostUrl = new URL(hrefAlbumFeed);
								String linkAlbumGoogle = albumPostUrl.toString();
								int demHinh = 0;
								for(int ii=0;ii < sizeImage;ii++){
									String src = imgs.get(ii).attr("src");
									try {
										Thread.sleep(3000);
										src = PicasawebAlbumUtil.uploadImagePicasa(src,myService,linkAlbumGoogle);
//										content += "<img alt='Truyenz.com  ảnh "+tenLoaiAnh+" "+anhDep.getName()+" "+ii+"' id='hinh"+ii+"' class='hinh' src='"+src+"/s0/truyenz.com'/><br />";
										content += src + "|";
										demHinh = demHinh + 1;
									} catch (Exception e) {
										logJsoupService.writeLog("ERROR - hoanvu.net",alias2+ " số " +ii+" lên picasa bị lỗi",loaiLog,true);
										break;
									}//end try {
								}//end for(int ii=0;ii < sizeImage;ii++){
								if(demHinh > 0){
									cad = new ContentAnhDep(name2,alias2,content,anhDep);
									contentAnhDepService.save(cad);
									anhDepService.updateIsGot(idAnhDep,true);
								}else{
									myAlbum.delete();
								}//end if(!content.equals("")){
							}//end if(cad == null){
						}//end if(imgs.size() > 0){
					} catch (Exception e) {
						logJsoupService.writeLog("ERROR - hoanvu.net","Lấy content album "+name2+" bị lỗi",loaiLog,true);
					}//end try catch
//				}else{
//					logJsoupService.writeLog("STOP - hoanvu.net","Ngưng lấy ảnh",loaiLog,true);
//					return;
//				}
			}//end for(int i = 0; i < sizeAll; i++){
		}//end if(lstAll.size() > 0){
		logJsoupService.writeLog("OK - hoanvu.net","Cập nhật xong content album ảnh đẹp",loaiLog,true);
	}//end public

	@Async
	@Override
	public void stopUpdateAlbum() {
		flagStopGetContentAnhDep = false;
	}

	
	/*************************************************************
	 * 
	 * lay truyen ma http://thegioima.com
	 * 
	 *************************************************************/
	
	@Async
	@Override
	public void getLinkSourceTruyenMaTGM() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup thegioima");
		int soureBy = 51;
		Account account = userService.findById((long) 1);
		TacGia tacGia = tacGiaService.findById((long) 5);
		String linkImage = "http://1.bp.blogspot.com/-4oYsOZRo7Ro/UElc1Si1o1I/AAAAAAAAAO4/GkoWJeYohQo";
		PicasawebService myService = new PicasawebService("zaza");
		try {
			myService.setUserCredentials("truyenzcom1@gmail.com", "11062010");
		} catch (AuthenticationException e1) {
			logJsoupService.writeLog("ERROR - Kết nối với picasa","kết nối picasa thất bại",loaiLog,true);
			e1.printStackTrace();
		}//end try {
		for(int i = 11; i > 0; i--){
			try {
				String url = "";
				if(i == 1){
					url = "http://thegioima.com/danhsach.html";
				}else{
					url = "http://thegioima.com/danhsach"+i+".html";
				}
				Document doc = Jsoup.connect(url)
						.timeout(1000000)
						.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					    .header("Accept-Encoding", "gzip, deflate")
					    .header("Accept-Language", "en-us,en;q=0.5")
					    .header("Cache-Control", "max-age=0")
					    .header("Connection", "keep-alive")
					    .header("Cookie", "__utma=224273374.1179175757.1355539420.1355539420.1355539420.1; __utmb=224273374.13.10.1355539420; __utmc=224273374; __utmz=224273374.1355539420.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); HstCfa1660365=1355539420798; HstCla1660365=1355540926883; HstCmu1660365=1355539420798; HstPn1660365=13; HstPt1660365=13; HstCnv1660365=1; HstCns1660365=2; __atuvc=10%7C50")
					    .header("Host", "thegioima.com")
					    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
					    .get();
				Elements divDanhSach = doc.select(".contentcenter > div");
//				int sizeDanhSach = divDanhSach.size();
				for(int j = 1 ; j > -1; j --){
					Element divItem = divDanhSach.eq(j).first();
					Elements liItems= divItem.select("ul li");
					int sizeLiItems = liItems.size() - 1;
					for(int jj = sizeLiItems ; jj > -1; jj --){
						Element item = liItems.get(jj);
						Element a = item.select("a").first();
						String href= a.attr("href");
						String name = a.text().replace("\"", "");
						String alias = Unsigned.boDau(name);
						TruyenMa truyen = truyenMaService.findByProperty("alias", alias);
						if(truyen==null){
							String nameImage = alias.replace(" ", "-")+".jpg";
							truyen = new TruyenMa(href,name,alias,soureBy,account,tacGia,linkImage,nameImage,new Date());
							truyenMaService.save(truyen);
						}//ket thuc if(comic==null){
					}//end for(int jj = sizeLiItems ; jj > -1; jj --){
				}//end for(int j = 0 ; j < sizeMenu; j ++){
			}catch (Exception e) {
				logJsoupService.writeLog("ERROR - thegioima.com","Lấy truyện ma bị lỗi",loaiLog,true);
				e.printStackTrace();
			}//ket thuc try catch
		}
	}

	@Async
	@Override
	public void getContentChapterTruyenMaTGM() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup thegioima");
		List<TruyenMa> lstTruyen = truyenMaService.getLstTruyenBySourceBy(51);
		if(lstTruyen.size() > 0){
			int sizeLstTruyen = lstTruyen.size();
			for(int i = 0; i < sizeLstTruyen; i++){
				TruyenMa truyen = lstTruyen.get(i);
				String linkSource = truyen.getLinkSource();
				try {
					Document doc = Jsoup.connect("http://thegioima.com/"+linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=224273374.1179175757.1355539420.1355539420.1355539420.1; __utmb=224273374.13.10.1355539420; __utmc=224273374; __utmz=224273374.1355539420.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); HstCfa1660365=1355539420798; HstCla1660365=1355540926883; HstCmu1660365=1355539420798; HstPn1660365=13; HstPt1660365=13; HstCnv1660365=1; HstCns1660365=2; __atuvc=10%7C50")
						    .header("Host", "thegioima.com")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element contentCenter = doc.select(".contentcenter").first();
					Elements page = contentCenter.select(".page");
					int chapterNumber = 1;
					Date postingTime = new Date();
					String nameChapterTruyen = "Chương "+ chapterNumber;
					String nameChapterUnsigned = Unsigned.boDau(nameChapterTruyen);
					ChapterTruyenMa chapterTruyen = chapterTruyenMaService.findAliasvTruyen(truyen, nameChapterUnsigned);
					if(page.size() > 0){
						Element liActive = page.select("ul li#active").first();
						Element nextLiActive = liActive.nextElementSibling();
						Element aHrefActive = nextLiActive.select("a").first();
						String href = aHrefActive.attr("href");
						int endIndex = linkSource.lastIndexOf("/")+1;
						linkSource = "http://thegioima.com/"+linkSource.substring(0, endIndex)+href;
						int chapterTamNumber = chapterNumber + 1;
						String nameChapterTam = "Chương "+chapterTamNumber; 
						String aliaseChapterTam = Unsigned.boDau(nameChapterTam);
						ChapterTam chapterTam = chapterTamService.findAliasvTruyen(truyen, aliaseChapterTam);
						if(chapterTam == null){
							chapterTam = new ChapterTam(truyen,nameChapterTam,aliaseChapterTam,linkSource,chapterTamNumber,51);
							chapterTamService.save(chapterTam);
						}
					}//end if(page.size() > 0){
					if(chapterTruyen == null){
						contentCenter.select("script").remove();
						contentCenter.select("noscript").remove();
						contentCenter.select("iframe").remove();
						contentCenter.select(".page").remove();
						String content = contentCenter.html().replace("thegioima.com", "truyenz.com").replace("thế giới ma chấm com", "truyenz.com");
						if(!content.equals("")){
							chapterTruyen = new ChapterTruyenMa(nameChapterTruyen,nameChapterUnsigned,chapterNumber,content,postingTime,truyen);
							chapterTruyenMaDao.save(chapterTruyen);
							ChapterTmpTruyenMa chapterTmpTruyenMa = new ChapterTmpTruyenMa(chapterTruyen.getId(),nameChapterTruyen,nameChapterUnsigned,chapterNumber,truyen);
							chapterTmpTruyenMaDao.save(chapterTmpTruyenMa);
							truyen.setTotalChapter(chapterNumber);
							truyenMaDao.merge(truyen);
						}//ket thuc if(!content.equals("")){
					}
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - thegioima.com","Cập nhật truyện ma "+truyen.getName(),loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//end for(int i = 0; i < sizeLstTruyen; i++){
		}//end if(lstTruyen.size() > 0){
	}//ket thuc ham
	
	@Async
	@Override
	public void getContentChapterTamTruyenMaTGM() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup thegioima");
		List<ChapterTam> lstChaptam = chapterTamService.getLstFromSourceBy(51);
		if(lstChaptam.size() > 0){
			int sizeLstChapter = lstChaptam.size();
			for(int i = 0; i < sizeLstChapter; i++){
				ChapterTam  chapterTam = lstChaptam.get(i);
				String linkSource = chapterTam.getLinkSource();
				TruyenMa truyen = chapterTam.getTruyenMa();
				try{
					Document doc = Jsoup.connect(linkSource)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "__utma=224273374.1179175757.1355539420.1355539420.1355539420.1; __utmb=224273374.13.10.1355539420; __utmc=224273374; __utmz=224273374.1355539420.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); HstCfa1660365=1355539420798; HstCla1660365=1355540926883; HstCmu1660365=1355539420798; HstPn1660365=13; HstPt1660365=13; HstCnv1660365=1; HstCns1660365=2; __atuvc=10%7C50")
						    .header("Host", "thegioima.com")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Element contentCenter = doc.select(".contentcenter").first();
					Elements page = contentCenter.select(".page");
					int chapterNumber = (int) chapterTam.getChapterNumber();
					Date postingTime = new Date();
					String nameChapterTruyen = "Chương "+ chapterNumber;
					String nameChapterUnsigned = Unsigned.boDau(nameChapterTruyen);
					ChapterTruyenMa chapterTruyen = chapterTruyenMaService.findAliasvTruyen(truyen, nameChapterUnsigned);
					if(page.size() > 0){
						Element liActive = page.select("ul li#active").first();
						Element nextLiActive = liActive.nextElementSibling();
						if(nextLiActive != null){
							Element aHrefActive = nextLiActive.select("a").first();
							String href = aHrefActive.attr("href");
							int endIndex = linkSource.lastIndexOf("/")+1;
							linkSource = linkSource.substring(0, endIndex)+href;
							int chapterTamNumber = chapterNumber + 1;
							String nameChapterTam = "Chương "+chapterTamNumber; 
							String aliaseChapterTam = Unsigned.boDau(nameChapterTam);
							ChapterTam chapterTamNew = chapterTamService.findAliasvTruyen(truyen, aliaseChapterTam);
							if(chapterTamNew == null){
								chapterTamNew = new ChapterTam(truyen,nameChapterTam,aliaseChapterTam,linkSource,chapterTamNumber,51);
								chapterTamService.save(chapterTamNew);
							}//end if(chapterTamNew == null){
						}//end if(nextLiActive != null){
					}//end if(page.size() > 0){
					if(chapterTruyen == null){
						contentCenter.select("script").remove();
						contentCenter.select("noscript").remove();
						contentCenter.select("iframe").remove();
						contentCenter.select(".page").remove();
						String content = contentCenter.html().replace("thegioima.com", "truyenz.com").replace("thế giới ma chấm com", "truyenz.com");;
						if(!content.equals("")){
							chapterTruyen = new ChapterTruyenMa(nameChapterTruyen,nameChapterUnsigned,chapterNumber,content,postingTime,truyen);
							chapterTruyenMaDao.save(chapterTruyen);
							ChapterTmpTruyenMa chapterTmpTruyenMa = new ChapterTmpTruyenMa(chapterTruyen.getId(),nameChapterTruyen,nameChapterUnsigned,chapterNumber,truyen);
							chapterTmpTruyenMaDao.save(chapterTmpTruyenMa);
							chapterTamService.delete(chapterTam.getId());
							truyen.setTotalChapter(chapterNumber);
							truyenMaDao.merge(truyen);
						}//ket thuc if(!content.equals("")){
					}
				}catch (Exception e) {
					logJsoupService.writeLog("ERROR - thegioima.com","Cập nhật truyện ma "+truyen.getName(),loaiLog,true);
					e.printStackTrace();
				}//ket thuc try catch
			}//end for(int i = 0; i < sizeLstTruyen; i++){
		}//end if(lstTruyen.size() > 0){
	}//ket thuc ham
	
	/*************************************************************
	 * 
	 * thivien.net
	 * 
	 *************************************************************/
	
	@Override
	public List<TapThoModel> getAlbumThiVien(String url) {
		List<TapThoModel> lstTapThoModel = new ArrayList<TapThoModel>();
		List<TapThoModel> lstTapThoModel1 = new ArrayList<TapThoModel>();
		try {
			Document doc = Jsoup.connect(url)
					.timeout(1000000)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
				    .header("Accept-Encoding", "gzip, deflate")
				    .header("Accept-Language", "en-us,en;q=0.5")
				    .header("Cache-Control", "max-age=0")
				    .header("Connection", "keep-alive")
				    .header("Cookie", "PHPSESSID=95f6c3199b1bbc2791ae0c4220cba3e3; __utma=150790643.290208375.1356091313.1356093278.1356095661.3; __utmc=150790643; __utmz=150790643.1356091313.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=150790643.1.10.1356095661")
				    .header("Host", "www.thivien.net")
				    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
				    .get();
		    Element tbody = doc.select("blockquote table tbody").first();
		    Elements tds = tbody.select("tr td");
		    if(tds.size() > 0){
		    	int sizeTd = tds.size();
		    	for(int i = 0 ; i < sizeTd ; i++){
		    		Element td = tds.get(i);
		    		Element span = td.select("span").first();
		    		if(span != null){
		    			String stt = span.text().replace(".", "");
			    		int stt1 = Integer.parseInt(stt);
			    		Element a = td.select("a").first();
			    		String link = a.attr("href");
			    		String name = a.text();
			    		TapThoModel tapThoModel = new TapThoModel(stt1,name,link);
			    		lstTapThoModel.add(tapThoModel);
		    		}
		    	}
		    }
		    if(lstTapThoModel.size() > 0){
		    	int sizeBaiTho = lstTapThoModel.size();
		    	for(int ii = 0 ; ii < sizeBaiTho ; ii++){
		    		TapThoModel ttm = lstTapThoModel.get(ii);
		    		String link = ttm.getLink();
		    		int endIndex = link.lastIndexOf("=")+1;
		    		String idBaiTho = link.substring(endIndex, link.length());
		    		idBaiTho = "#PoemBody"+idBaiTho;
		    		try {
		    			Thread.sleep(3000);
		    			Document doc1 = Jsoup.connect("http://www.thivien.net"+link)
		    					.timeout(1000000)
		    					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
		    				    .header("Accept-Encoding", "gzip, deflate")
		    				    .header("Accept-Language", "en-us,en;q=0.5")
		    				    .header("Connection", "keep-alive")
		    				    .header("Cookie", "PHPSESSID=95f6c3199b1bbc2791ae0c4220cba3e3; __utma=150790643.290208375.1356091313.1356095661.1356097673.4; __utmc=150790643; __utmz=150790643.1356091313.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmb=150790643.2.10.1356097673")
		    				    .header("Host", "www.thivien.net")
		    				    .header("Referer",url)
		    				    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
		    				    .get();
		    			String contentBaiTho = doc1.select(idBaiTho).first().html();
		    			ttm.setContent(contentBaiTho);
		    			lstTapThoModel1.add(ttm);
					} catch (Exception e) {
						e.printStackTrace();
					}
		    	}
		    }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lstTapThoModel1;
	}

	/*************************************************************
	 * 
	 * cap nhat the loai tu tunghoanh.com
	 * 
	 *************************************************************/
	@Async
	@Override
	public void getCategoryTruyenKiemHiepFromTungHoanh() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			for(int j=1;j<21;j++){
				Document doc = Jsoup.connect("http://tunghoanh.com/truyen/moi-cap-nhat/trang-"+j+".html")
						.timeout(1000000)
						.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					    .header("Accept-Encoding", "gzip, deflate")
					    .header("Accept-Language", "en-us,en;q=0.5")
					    .header("Cache-Control", "max-age=0")
					    .header("Connection", "keep-alive")
					    .header("Cookie", "ann_show=1; location.href=1; PHPSESSID=e4v2doah40glbh01ck4h7kant3; __utma=180264865.1753647994.1356446610.1356446610.1356446610.1; __utmb=180264865.2.10.1356446610; __utmc=180264865; __utmz=180264865.1356446610.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ver=; __RC=5; __R=3; _azs=")
					    .header("Host", "tunghoanh.com")
					    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
					    .get();
				Elements li = doc.select("#box_10 li");
				if(li.size() > 0){
					int sizeList = li.size();
					for(int i = 0; i < sizeList ; i++){
						try {
							Element item = li.get(i);
							Element a = item.select(".td2 a").first();
							String nameTruyen = a.text();
							String alias = Unsigned.boDau(nameTruyen);
							TruyenKiemHiep truyen = truyenKiemHiepService.findByProperty("alias", alias);
							if(truyen != null){
								String category = truyen.getCategories();
								if(category != null){
									String linkSource = a.attr("href");
									linkSource = linkSource.replace(" ", "%20");
									truyen.setTranslator(linkSource);
									truyenKiemHiepDao.merge(truyen);
								}
							}//ket thuc if(truyen == null){
						} catch (Exception e) {
							e.printStackTrace();
						}
					}//ket thuc for(int i = 0; i < sizeList ; i++){
				}//ket thuc if(li.size() > 0){
			}//end for(int j=1;j<21;j++){
			logJsoupService.writeLog("OK - tunghoanh.com - Lấy truyện","Đã lấy xong link truyện kiếm hiệp",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - tunghoanh.com   - Lấy truyện","Có lỗi xảy ra khi lấy truyện kiếm hiệp",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
	}

	@Async
	@Override
	public void getCategoryTruyenTrinhThamFromTungHoanh() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			for(int j=1;j<21;j++){
				Document doc = Jsoup.connect("http://tunghoanh.com/truyen/moi-cap-nhat/trang-"+j+".html")
						.timeout(1000000)
						.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					    .header("Accept-Encoding", "gzip, deflate")
					    .header("Accept-Language", "en-us,en;q=0.5")
					    .header("Cache-Control", "max-age=0")
					    .header("Connection", "keep-alive")
					    .header("Cookie", "ann_show=1; location.href=1; PHPSESSID=e4v2doah40glbh01ck4h7kant3; __utma=180264865.1753647994.1356446610.1356446610.1356446610.1; __utmb=180264865.2.10.1356446610; __utmc=180264865; __utmz=180264865.1356446610.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ver=; __RC=5; __R=3; _azs=")
					    .header("Host", "tunghoanh.com")
					    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
					    .get();
				Elements li = doc.select("#box_10 li");
				if(li.size() > 0){
					int sizeList = li.size();
					for(int i = 0; i < sizeList ; i++){
						try {
							Element item = li.get(i);
							Element a = item.select(".td2 a").first();
							String nameTruyen = a.text();
							String alias = Unsigned.boDau(nameTruyen);
							TruyenTrinhTham truyen = truyenTrinhThamService.findByProperty("alias", alias);
							if(truyen != null){
								String category = truyen.getCategories();
								if(category != null){
									String linkSource = a.attr("href");
									linkSource = linkSource.replace(" ", "%20");
									truyen.setTranslator(linkSource);
									truyenTrinhThamDao.merge(truyen);
								}
							}//ket thuc if(truyen == null){
						} catch (Exception e) {
							e.printStackTrace();
						}
					}//ket thuc for(int i = 0; i < sizeList ; i++){
				}//ket thuc if(li.size() > 0){
			}//end for(int j=1;j<21;j++){
			logJsoupService.writeLog("OK - tunghoanh.com - Lấy truyện","Đã lấy xong link truyện trinh thám",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - tunghoanh.com   - Lấy truyện","Có lỗi xảy ra khi lấy truyện trinh thám",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
	}

	@Async
	@Override
	public void getCategoryTruyenTinhCamFromTungHoanh() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			for(int j=1;j<21;j++){
				Document doc = Jsoup.connect("http://tunghoanh.com/truyen/moi-cap-nhat/trang-"+j+".html")
						.timeout(1000000)
						.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					    .header("Accept-Encoding", "gzip, deflate")
					    .header("Accept-Language", "en-us,en;q=0.5")
					    .header("Cache-Control", "max-age=0")
					    .header("Connection", "keep-alive")
					    .header("Cookie", "ann_show=1; location.href=1; PHPSESSID=e4v2doah40glbh01ck4h7kant3; __utma=180264865.1753647994.1356446610.1356446610.1356446610.1; __utmb=180264865.2.10.1356446610; __utmc=180264865; __utmz=180264865.1356446610.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ver=; __RC=5; __R=3; _azs=")
					    .header("Host", "tunghoanh.com")
					    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
					    .get();
				Elements li = doc.select("#box_10 li");
				if(li.size() > 0){
					int sizeList = li.size();
					for(int i = 0; i < sizeList ; i++){
						try {
							Element item = li.get(i);
							Element a = item.select(".td2 a").first();
							String nameTruyen = a.text();
							String alias = Unsigned.boDau(nameTruyen);
							TruyenTinhCam truyen = truyenTinhCamService.findByProperty("alias", alias);
							if(truyen != null){
								String category = truyen.getCategories();
								if(category != null){
									String linkSource = a.attr("href");
									linkSource = linkSource.replace(" ", "%20");
									truyen.setTranslator(linkSource);
									truyenTinhCamDao.merge(truyen);
								}
							}//ket thuc if(truyen == null){
						} catch (Exception e) {
							e.printStackTrace();
						}
					}//ket thuc for(int i = 0; i < sizeList ; i++){
				}//ket thuc if(li.size() > 0){
			}//end for(int j=1;j<21;j++){
			logJsoupService.writeLog("OK - tunghoanh.com - Lấy truyện","Đã lấy xong link truyện tình cảm",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - tunghoanh.com   - Lấy truyện","Có lỗi xảy ra khi lấy truyện tình cảm",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
	}
	
	//cap nhat the loai truyen chu
	
	@Async
	@Override
	public void getCategoryTruyenKiemHiepByLinkFromTungHoanh() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			List<TruyenKiemHiep> lstTruyen = truyenKiemHiepService.findByPropertyNotNull("translator");
			int sizeTruyen = lstTruyen.size();
			for(int j=0;j<sizeTruyen;j++){
				try {
					TruyenKiemHiep truyen = lstTruyen.get(j);
					String link = truyen.getTranslator();
					Document doc = Jsoup.connect(link)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "location.href=1; PHPSESSID=e4v2doah40glbh01ck4h7kant3; __utma=180264865.1753647994.1356446610.1356446610.1356446610.1; __utmb=180264865.3.10.1356446610; __utmc=180264865; __utmz=180264865.1356446610.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ver=; __RC=5; __R=3; _azs=")
						    .header("Host", "tunghoanh.com")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Elements box = doc.select(".info_left");
					Element span = box.select("p").last().select("span").first();
					String categories = span.text();
					Elements a = span.select("a");
					int sizeA = a.size();
					if(sizeA > 0){
						for(int z = 0; z < sizeA ; z++){
							String nameLoaiTruyenChu = a.get(z).text();
							String aliasLoaiTruyenChu = Unsigned.boDau(nameLoaiTruyenChu);
							int type = 2;
							LoaiTruyenChu loaiTruyenChu = loaiTruyenChuService.findByAliasVType(aliasLoaiTruyenChu,type);
							if(loaiTruyenChu == null){
								loaiTruyenChu = new LoaiTruyenChu(nameLoaiTruyenChu,aliasLoaiTruyenChu,type);
								loaiTruyenChuService.save(loaiTruyenChu);
							}//end if(loaiTruyenChu == null){
							SearchLoaiTruyenKiemHiep sltt = searchLoaiTruyenKiemHiepService.findByLoaiTruyenKiemHiepVTruyenKiemHiep(loaiTruyenChu,truyen);
							if(sltt == null){
								sltt = new SearchLoaiTruyenKiemHiep(loaiTruyenChu,truyen);
								searchLoaiTruyenKiemHiepService.save(sltt);
							}//end if(sltt == null){
						}//end for(int z = 0; z < sizeA ; z++){
					}//end if(sizeA > 0){
					truyen.setCategories(categories);
					truyen.setTranslator(null);
					truyenKiemHiepDao.merge(truyen);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}//end for(int j=1;j<21;j++){
			logJsoupService.writeLog("OK - tunghoanh.com - Cập nhật thể loại truỵện","Đã cập nhật xong thể loại truyện kiếm hiệp",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - tunghoanh.com   - Cập nhật thể loại truỵện","Có lỗi xảy ra khi cập nhật thể loại truyện truyện kiếm hiệp",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
	}

	@Async
	@Override
	public void getCategoryTruyenTrinhThamByLinkFromTungHoanh() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			List<TruyenTrinhTham> lstTruyen = truyenTrinhThamService.findByPropertyNotNull("translator");
			int sizeTruyen = lstTruyen.size();
			for(int j=0;j<sizeTruyen;j++){
				try {
					TruyenTrinhTham truyen = lstTruyen.get(j);
					String link = truyen.getTranslator();
					Document doc = Jsoup.connect(link)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "location.href=1; PHPSESSID=e4v2doah40glbh01ck4h7kant3; __utma=180264865.1753647994.1356446610.1356446610.1356446610.1; __utmb=180264865.3.10.1356446610; __utmc=180264865; __utmz=180264865.1356446610.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ver=; __RC=5; __R=3; _azs=")
						    .header("Host", "tunghoanh.com")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Elements box = doc.select(".info_left");
					Element span = box.select("p").last().select("span").first();
					String categories = span.text();
					Elements a = span.select("a");
					int sizeA = a.size();
					if(sizeA > 0){
						for(int z = 0; z < sizeA ; z++){
							String nameLoaiTruyenChu = a.get(z).text();
							String aliasLoaiTruyenChu = Unsigned.boDau(nameLoaiTruyenChu);
							int type = 4;
							LoaiTruyenChu loaiTruyenChu = loaiTruyenChuService.findByAliasVType(aliasLoaiTruyenChu,type);
							if(loaiTruyenChu == null){
								loaiTruyenChu = new LoaiTruyenChu(nameLoaiTruyenChu,aliasLoaiTruyenChu,type);
								loaiTruyenChuService.save(loaiTruyenChu);
							}//end if(loaiTruyenChu == null){
							SearchLoaiTruyenTrinhTham sltt = searchLoaiTruyenTrinhThamService.findByLoaiTruyenTrinhThamVTruyenTrinhTham(loaiTruyenChu,truyen);
							if(sltt == null){
								sltt = new SearchLoaiTruyenTrinhTham(loaiTruyenChu,truyen);
								searchLoaiTruyenTrinhThamService.save(sltt);
							}//end if(sltt == null){
						}//end for(int z = 0; z < sizeA ; z++){
					}//end if(sizeA > 0){
					truyen.setCategories(categories);
					truyen.setTranslator(null);
					truyenTrinhThamDao.merge(truyen);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}//end for(int j=1;j<21;j++){
			logJsoupService.writeLog("OK - tunghoanh.com - Cập nhật thể loại truỵện","Đã cập nhật xong thể loại truyện trinh thám",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - tunghoanh.com   - Cập nhật thể loại truỵện","Có lỗi xảy ra khi cập nhật thể loại truyện truyện trinh thám",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
	}

	@Async
	@Override
	public void getCategoryTruyenTinhCamByLinkFromTungHoanh() {
		LoaiLog loaiLog = loaiLogService.findByProperty("alias","jsoup truyen hixxinfo");
		try {
			List<TruyenTinhCam> lstTruyen = truyenTinhCamService.findByPropertyNotNull("translator");
			int sizeTruyen = lstTruyen.size();
			for(int j=0;j<sizeTruyen;j++){
				try {
					TruyenTinhCam truyen = lstTruyen.get(j);
					String link = truyen.getTranslator();
					Document doc = Jsoup.connect(link)
							.timeout(1000000)
							.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
						    .header("Accept-Encoding", "gzip, deflate")
						    .header("Accept-Language", "en-us,en;q=0.5")
						    .header("Cache-Control", "max-age=0")
						    .header("Connection", "keep-alive")
						    .header("Cookie", "location.href=1; PHPSESSID=e4v2doah40glbh01ck4h7kant3; __utma=180264865.1753647994.1356446610.1356446610.1356446610.1; __utmb=180264865.3.10.1356446610; __utmc=180264865; __utmz=180264865.1356446610.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ver=; __RC=5; __R=3; _azs=")
						    .header("Host", "tunghoanh.com")
						    .header("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0)")
						    .get();
					Elements box = doc.select(".info_left");
					Element span = box.select("p").last().select("span").first();
					String categories = span.text();
					Elements a = span.select("a");
					int sizeA = a.size();
					if(sizeA > 0){
						for(int z = 0; z < sizeA ; z++){
							String nameLoaiTruyenChu = a.get(z).text();
							String aliasLoaiTruyenChu = Unsigned.boDau(nameLoaiTruyenChu);
							int type = 3;
							LoaiTruyenChu loaiTruyenChu = loaiTruyenChuService.findByAliasVType(aliasLoaiTruyenChu,type);
							if(loaiTruyenChu == null){
								loaiTruyenChu = new LoaiTruyenChu(nameLoaiTruyenChu,aliasLoaiTruyenChu,type);
								loaiTruyenChuService.save(loaiTruyenChu);
							}//end if(loaiTruyenChu == null){
							SearchLoaiTruyenTinhCam sltt = searchLoaiTruyenTinhCamService.findByLoaiTruyenTinhCamVTruyenTinhCam(loaiTruyenChu,truyen);
							if(sltt == null){
								sltt = new SearchLoaiTruyenTinhCam(loaiTruyenChu,truyen);
								searchLoaiTruyenTinhCamService.save(sltt);
							}//end if(sltt == null){
						}//end for(int z = 0; z < sizeA ; z++){
					}//end if(sizeA > 0){
					truyen.setCategories(categories);
					truyen.setTranslator(null);
					truyenTinhCamDao.merge(truyen);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}//end for(int j=1;j<21;j++){
			logJsoupService.writeLog("OK - tunghoanh.com - Cập nhật thể loại truỵện","Đã cập nhật xong thể loại truyện tình cảm",loaiLog,false);
		} catch (Exception e) {
			logJsoupService.writeLog("ERROR - tunghoanh.com   - Cập nhật thể loại truỵện","Có lỗi xảy ra khi cập nhật thể loại truyện truyện tình cảm",loaiLog,true);
			e.printStackTrace();
		}//ket thuc try catch
	}
	
//	public static void main(String[] args) throws Exception {
//		System.out.println(System.getProperty("java.io.tmpdir"));
//	}

}
